How To check if a file exists?
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

How To check if a file exists?

Started by Dessamator, 06 April, 2005, 19:53:46

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dessamator

i was just wondering if this  :
 
function verify(filename)
local f = io.open(filename, "r")
	if f then
		f:close()
		return "true"
	else
		return "false"
	end
end
 


is the best and fastest way and most efficient to check if a file exists or not?
Ignorance is Bliss.

Sedulus

sounds about right,
but returning the strings "true" and "false" is confusing and unnecessary overhead.
use nil (or false) and 1 (or true).
that said, you can skip the whole 'else' part. reaching the function body end, you'll return nil anyway.

Dessamator

QuoteOriginally posted by Sedulus
sounds about right,
but returning the strings "true" and "false" is confusing and unnecessary overhead.
use nil (or false) and 1 (or true).
that said, you can skip the whole 'else' part. reaching the function body end, you'll return nil anyway.

yaps , i agree, is this :
  
function verify(filename)
local f = io.open(filename, "r")
	if f then
		f:close()
		return "true"
	end
end
     


better?
Ignorance is Bliss.

Sedulus

almost...
string.gsub( yourCode, '"true"', 'true' )

Dessamator

         
almost...

string.gsub( yourCode, '"true"', 'true' )


hmm, i c :
         
function verify(filename)
local f = io.open(filename, "r")
	if f then
		f:close()
		return true
	end
end


Done !!(hopefully)
Ignorance is Bliss.

Herodes

Sedulus,... I think that returning true / false is one of the best ways around (note btw that they are reserved keywords in Lua so they aren't getting in the way of variables.) The expressions in 'if (expr) then' are evaluated to true or false nil is translated to false too. But I think that it all comes down to how each one of us is used to writing his/her scripts ...

Dessamator

QuoteOriginally posted by Herodes
Sedulus,... I think that returning true / false is one of the best ways around (note btw that they are reserved keywords in Lua so they aren't getting in the way of variables.) The expressions in 'if (expr) then' are evaluated to true or false nil is translated to false too. But I think that it all comes down to how each one of us is used to writing his/her scripts ...

hmm, indeed i agree with that too, but the point is, how to find if a file exists using the least amount of code, and doing that efficiently  !!
Ignorance is Bliss.

Herodes

QuoteOriginally posted by Dessamator
... how to find if a file exists using the least amount of code, and doing that efficiently  !!
Your last piece is the way I'd do it myself too.. but I haven't gone into the guts of the io lib functions of Lua.. so I couldn't say for sure what is the bestestestest way to do it. (This one will work just fine too ;)

Dessamator

QuoteOriginally posted by Herodes
QuoteOriginally posted by Dessamator
... how to find if a file exists using the least amount of code, and doing that efficiently  !!
Your last piece is the way I'd do it myself too.. but I haven't gone into the guts of the io lib functions of Lua.. so I couldn't say for sure what is the bestestestest way to do it. (This one will work just fine too ;)

well, we'll just have to settle for the next "bestestestest" thing, :D
Ignorance is Bliss.

SMF spam blocked by CleanTalk