How to check if a file exist?
How to check if a file exist?
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
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
Code: Select all
If ReadFile(0,filenam$)
; file exists
Else
; file does not exist
EndIf
PB 5.40 LTS, W7,8,10 64bit and Mint x64
no need to open the file for reading
Code: Select all
If FileSize(FileName$) = -1
; file does not exist
endif
Or:
To check for a file:
To check for a folder:
To check for a file:
Code: Select all
If ExamineDirectory(1, ".", filename$) = 0
; file does not exist
Else
; file exists
EndIf
Code: Select all
If ExamineDirectory(1, foldername$, "*.*") = 0
; folder does not exist
Else
; folder exists
EndIf
-
- Enthusiast
- Posts: 218
- Joined: Sat Apr 26, 2003 4:47 am
- Location: Dullsville..population: me
- Contact:
Nope..that routine could fail if another app has already opened the file in exclusive mode..wichtel wrote:Code: Select all
If ReadFile(0,filenam$) ; file exists Else ; file does not exist EndIf
-
- Enthusiast
- Posts: 169
- Joined: Sat Mar 14, 2015 11:53 am
Re: How to check if a file or folder exists?
I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:
Cheers!
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
- NicTheQuick
- Addict
- Posts: 1224
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: How to check if a file or folder exists?
If you examine a directory you always have to finish it too with FinishDirectory(). You just created a memory leak.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:Cheers!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
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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: How to check if a file exist?
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)
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?
A little bit betterStarWarsFan 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:Cheers!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
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?
Macro is enough ...
Code: Select all
Macro ExistsDirectory(_directory_)
Bool(FileSize(_directory_) = -2)
EndMacro
Macro ExistsFile(_file_)
Bool(FileSize(_file_) > -1)
EndMacro
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive