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.
Is there an easy way to check if File.txt exists?
Is there an easy way to check if File.txt exists?
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.
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?
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.
▓▓▓▓▓▒▒▒▒▒░░░░░
Re: Is there an easy way to check if File.txt exists?
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
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?
Thanks but is this safe to use with an executable?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
▓▓▓▓▓▒▒▒▒▒░░░░░
Re: Is there an easy way to check if File.txt exists?
Since the description is "Open an existing file for read-only operations", it should be. It doesn't do any actual reading.Nituvious wrote:Thanks but is this safe to use with an executable?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
cheers
- Fangbeast
- PureBasic Protozoa

- Posts: 4799
- 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?
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
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
- Michael Vogel
- Addict

- Posts: 2840
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Is there an easy way to check if File.txt exists?
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)
EndIfRe: Is there an easy way to check if File.txt exists?
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.
- Michael Vogel
- Addict

- Posts: 2840
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Is there an easy way to check if File.txt exists?
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
EndProcedureCode: 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
EndProcedureRe: Is there an easy way to check if File.txt exists?
With these functions you can not use #PB_Any, because you would need the result of ReadFile() to Close the opened file.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 ...
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
PB Last Final / Last Beta Testing
- Michael Vogel
- Addict

- Posts: 2840
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Is there an easy way to check if File.txt exists?
helpy wrote:With these functions you can not use #PB_Any, because you would need the result of ReadFile() to Close the opened file.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 ...
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
EndProcedureRe: Is there an easy way to check if File.txt exists?
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?
To just check the file existance, FileSize() is faster (hey Fang !).
Re: Is there an easy way to check if File.txt exists?
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.
Re: Is there an easy way to check if File.txt exists?
Instead of:...or:... otherwise FileSize() is called twice, if the file exists
...you can write:SFSxOI wrote:Code: Select all
If FileSize("C:\some_file.txt") <> -1 And FileSize("C:\some_file.txt") <> -2 ;(...) EndIf
Code: Select all
If FileSize("C:\some_file.txt") > -1
;(...)
EndIf
Code: Select all
If FileSize("C:\some_file.txt") >= 0
;(...)
EndIf
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
PB Last Final / Last Beta Testing

