Page 1 of 1
Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 3:49 am
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.

Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 3:56 am
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?
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 3:57 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 3:59 am
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?
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 4:37 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 5:19 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 7:32 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 9:29 am
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.
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 9:40 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 10:44 am
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.
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 11:09 am
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
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 2:30 pm
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!
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 3:36 pm
by Fred
To just check the file existance, FileSize() is faster (hey Fang !).
Re: Is there an easy way to check if File.txt exists?
Posted: Thu Dec 10, 2009 11:43 pm
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
Re: Is there an easy way to check if File.txt exists?
Posted: Fri Dec 11, 2009 8:00 am
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