Page 1 of 1

How to check if a file exist?

Posted: Thu Jul 17, 2003 2:55 pm
by Hurga
Hi
I m a complete newby to purebasic. And i cant find a commmand to check if a file is already existing or not
Can s o help me please?

Thx

Posted: Thu Jul 17, 2003 3:01 pm
by Pupil
Check out the FileSize() command..

Posted: Thu Jul 17, 2003 3:04 pm
by wichtel

Code: Select all

If ReadFile(0,filenam$)
  ; file exists
Else
  ; file does not exist
EndIf

Posted: Thu Jul 17, 2003 3:09 pm
by Berikco
no need to open the file for reading

Code: Select all

If FileSize(FileName$) = -1
; file does not exist
endif

Posted: Thu Jul 17, 2003 8:20 pm
by Kale
Or:

To check for a file:

Code: Select all

If ExamineDirectory(1, ".", filename$) = 0
    ; file does not exist
Else
    ; file exists
EndIf
To check for a folder:

Code: Select all

If ExamineDirectory(1, foldername$, "*.*") = 0
    ; folder does not exist
Else
    ; folder exists
EndIf
:twisted:

Posted: Fri Jul 18, 2003 2:03 am
by Doobrey
wichtel wrote:

Code: Select all

If ReadFile(0,filenam$)
  ; file exists
Else
  ; file does not exist
EndIf
Nope..that routine could fail if another app has already opened the file in exclusive mode..

Posted: Fri Jul 18, 2003 8:06 am
by wichtel
learned :)

I prefer now Kale's version.

Posted: Fri Jul 18, 2003 9:39 am
by Fred
The preferred way is the FileSize() one (faster, smaller..).

Re: How to check if a file or folder exists?

Posted: Mon Sep 06, 2021 10:23 am
by StarWarsFan
I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!

Re: How to check if a file or folder exists?

Posted: Mon Sep 06, 2021 10:57 am
by NicTheQuick
StarWarsFan wrote: Mon Sep 06, 2021 10:23 am I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!
If you examine a directory you always have to finish it too with FinishDirectory(). You just created a memory leak.

Also if you read the help you can see that FileSize returns -2 if the path is a directory. So there is really no need for ExamineDirectory in the first place.

Re: How to check if a file or folder exists?

Posted: Mon Sep 06, 2021 11:08 am
by Kiffi
NicTheQuick wrote: Mon Sep 06, 2021 10:57 am
StarWarsFan wrote: Mon Sep 06, 2021 10:23 am...
...
plus the unnecessary use of the variable 'r'.

Re: How to check if a file exist?

Posted: Mon Sep 06, 2021 11:32 am
by highend
So there is really no need for ExamineDirectory in the first place
There can be?

Try to find out (e.g. on Windows 7) if a file or folder in an overlong path exists.

FileSize() will report that it does not while ExamineDirectory() shows the correct result

Ofc the path is prefixed with "\\?\" | "\\?\UNC" (on a local / UNC path)

Re: How to check if a file or folder exists?

Posted: Mon Sep 06, 2021 1:47 pm
by Cyllceaux
StarWarsFan wrote: Mon Sep 06, 2021 10:23 am I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!
A little bit better

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn Bool(FileSize(dir$) = -2): EndProcedure
Procedure ExistFILE(file$) : ProcedureReturn Bool(FileSize(file$) > -1) : EndProcedure

Re: How to check if a file exist?

Posted: Mon Sep 06, 2021 6:44 pm
by mk-soft
Macro is enough ...

Code: Select all

Macro ExistsDirectory(_directory_)
  Bool(FileSize(_directory_) = -2)
EndMacro

Macro ExistsFile(_file_)
  Bool(FileSize(_file_) > -1)
EndMacro