Is there an easy way to check if File.txt exists?

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Is there an easy way to check if File.txt exists?

Post by Nituvious »

Is there any easy way for us to check if for example, FileA.txt exist, and if it doesn't then use FileB.txt?
I have been looking through the FileSystem and File reference in the help file but I don't seem to find anything relevant. I probably am not looking hard enough though. :?
▓▓▓▓▓▒▒▒▒▒░░░░░
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Is there an easy way to check if File.txt exists?

Post by Nituvious »

Sorry for the double post, I just used ReadFile(#,String$) and I got what I needed. Does anyone know ReadFile is safe to use with executables?
Last edited by Nituvious on Thu Dec 10, 2009 3:58 am, edited 1 time in total.
▓▓▓▓▓▒▒▒▒▒░░░░░
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Is there an easy way to check if File.txt exists?

Post by rsts »

If readFile(0,"fileA.txt")
do fileA stuff
else
If readFile(0,"fileB.txt")
do fileB stuff
else
No file B either :(
EndIF
EndIF

something like this?

cheers
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Is there an easy way to check if File.txt exists?

Post by Nituvious »

rsts wrote:If readFile(0,"fileA.txt")
do fileA stuff
else
If readFile(0,"fileB.txt")
do fileB stuff
else
No file B either :(
EndIF
EndIF

something like this?

cheers
Thanks but is this safe to use with an executable?
▓▓▓▓▓▒▒▒▒▒░░░░░
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Is there an easy way to check if File.txt exists?

Post by rsts »

Nituvious wrote:
rsts wrote:If readFile(0,"fileA.txt")
do fileA stuff
else
If readFile(0,"fileB.txt")
do fileB stuff
else
No file B either :(
EndIF
EndIF

something like this?

cheers
Thanks but is this safe to use with an executable?
Since the description is "Open an existing file for read-only operations", it should be. It doesn't do any actual reading.

cheers
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Is there an easy way to check if File.txt exists?

Post by Fangbeast »

What's wrong with the FileSize command? A value of -1 will tell you that the file does not exist and you do not need to open it first.

From the manual

Code: Select all

Syntax

Result.q = FileSize(Filename$)

Description

Returns the size of the specified file. 

Special 'Result' values: 
  -1: File not found.
  -2: File is a directory.

Supported OS 

All
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Is there an easy way to check if File.txt exists?

Post by Michael Vogel »

Not tested, but should work...

Code: Select all

Procedure.l OpenFirstFile(n,a.s,b.s="")
	If FileSize(a)>=0
		ReadFile(n,a)
		ProcedureReturn 1
	ElseIf FileSize(b)>=0
		ReadFile(n,b)
		ProcedureReturn 2
	Else
		ProcedureReturn 0
	EndIf
EndProcedure

If OpenFirstFile(7,"TextA.txt","TextB.txt")

	Debug ReadString(7)
	CloseFile(7)
EndIf
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is there an easy way to check if File.txt exists?

Post by Fred »

If you want to use ReadFile(), no need to check it before, it's double check. Also, ALWAYS check the return of ReadFile, it could exists but be locked by an application.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Is there an easy way to check if File.txt exists?

Post by Michael Vogel »

Depending if the first readable file should be opened or the first existent should be tried to open there are some variants possible :)...

Code: Select all

Procedure.l OpenFirstFile(n,a.s,b.s="")

	If ReadFile(n,a)
		ProcedureReturn 1
	ElseIf ReadFile(n,b)
		ProcedureReturn 2
	Else
		ProcedureReturn 0
	EndIf

EndProcedure

Code: Select all

Procedure.l OpenFirstFile(n,a.s,b.s="")

	If FileSize(a)>=0
		If ReadFile(n,a)
			ProcedureReturn 1
		EndIf

	ElseIf FileSize(b)>=0
		If ReadFile(n,b)
			ProcedureReturn 2
		EndIf
	EndIf
	
	ProcedureReturn 0
	
EndProcedure
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Is there an easy way to check if File.txt exists?

Post by helpy »

Michael Vogel wrote:Depending if the first readable file should be opened or the first existent should be tried to open there are some variants possible ...
With these functions you can not use #PB_Any, because you would need the result of ReadFile() to Close the opened file.
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Is there an easy way to check if File.txt exists?

Post by Michael Vogel »

helpy wrote:
Michael Vogel wrote:Depending if the first readable file should be opened or the first existent should be tried to open there are some variants possible ...
With these functions you can not use #PB_Any, because you would need the result of ReadFile() to Close the opened file.

Code: Select all

Procedure.l OpenFirstFile(n,a.s,b.s="")

	Protected Result

	Result=ReadFile(n,a)

	If Result
		ProcedureReturn Result
	Else
		Result=ReadFile(n,b)
	EndIf

	ProcedureReturn Result

EndProcedure
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Is there an easy way to check if File.txt exists?

Post by Nituvious »

ReadFile() does the trick! I needed someway of checking if one of two files existed, if one did it would set the appropriate file path. Thank you guys for all of the help!
▓▓▓▓▓▒▒▒▒▒░░░░░
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is there an easy way to check if File.txt exists?

Post by Fred »

To just check the file existance, FileSize() is faster (hey Fang !).
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Is there an easy way to check if File.txt exists?

Post by SFSxOI »

Code: Select all

If FileSize("C:\some_file.txt") <> -1 And FileSize("C:\some_file.txt") <> -2
MessageRequester("Information", "Path set", #PB_MessageRequester_Ok)
;set path here
Else
MessageRequester("Information", "file not found", #PB_MessageRequester_Ok)
; file does not exist
EndIf
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Is there an easy way to check if File.txt exists?

Post by helpy »

Instead of:
SFSxOI wrote:

Code: Select all

If FileSize("C:\some_file.txt") <> -1 And FileSize("C:\some_file.txt") <> -2
  ;(...)
EndIf
...you can write:

Code: Select all

If FileSize("C:\some_file.txt") > -1
  ;(...)
EndIf
...or:

Code: Select all

If FileSize("C:\some_file.txt") >= 0
  ;(...)
EndIf
... otherwise FileSize() is called twice, if the file exists
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply