What is the fastest way of cheking if a file exists?
Posted: Mon May 29, 2006 4:15 am
What is the fastest way of cheking if a file exists?
I currently use this: If FileSize(File) > 0
I currently use this: If FileSize(File) > 0
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
If ExamineDirectory(0,GetCurrentDirectory(),"Sqlite3.dll") = 0
; it isn't there, handle condition
Else
; it is there, handle condition
Endif
Better check for >-1 because a file can be a size of 0 bytes........Joakim Christiansen wrote:What is the fastest way of cheking if a file exists?
I currently use this: If FileSize(File) > 0
Code: Select all
Procedure FileExists(FileName.s)
Protected Result.l, Find.WIN32_FIND_DATA
Result = FindFirstFile_(@FileName, Find)
FindClose_(Result)
If Result <> #INVALID_HANDLE_VALUE
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Code: Select all
Import "ntdll.lib"
RtlDoesFileExists_U(file.p-unicode)
EndImport
Debug RtlDoesFileExists_U("c:\your.file")
Code: Select all
Debug PathFileExists_("c:\program files\internet explorer\iexplore.exe")
Debug PathFileExists_("c:\program files\internet explorer\iexplore.exee")
That's correct. Nevertheless bingo's code even works with WinNT4 SP6...Rescator wrote:Yeah, but that one is not documented in the PSDK and not guaranteed to exist in the future at all or OS variants. Use with caution.
Code: Select all
Procedure.l Exist(File$) ;- Check a drive + file exists, without system requesters etc.
; Check if a drive or drive+file exists
; Return -1 if exists, else 0
; *** Never *** displays a system error even if device is missing
; such as a multi card interface with no cards plugged in.
Protected EFlag.l, OldErrorMode.l, Junk.l
OldErrorMode = SetErrorMode_(1) ; Turn off screen error messages
If GetFileAttributes_(@File$)=-1 ; Get file butes. -1 = fail
Junk.l=GetLastError_() ; Get last error, to flush system
SetLastError_(0) ; Set error to zero
EFlag.l = 0 ; Return value to flag FAIL
Else
EFlag.l = -1 ; Return value to flag a PASS
EndIf
SetErrorMode_(OldErrorMode) ; Reset the error flags
ProcedureReturn EFlag
EndProcedure