What is the fastest way of cheking if a file exists?
- Joakim Christiansen
- Addict
- Posts: 2452
- Joined: Wed Dec 22, 2004 4:12 pm
- Location: Norway
- Contact:
What is the fastest way of cheking if a file exists?
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
I like logic, hence I dislike humans but love computers.
- travismcgee
- New User
- Posts: 9
- Joined: Mon May 29, 2006 2:16 am
Here's a line from one of my programs, dunno if you'll like it or not:
I have no idea how fast it is.
Code: Select all
If ExamineDirectory(0,GetCurrentDirectory(),"Sqlite3.dll") = 0
; it isn't there, handle condition
Else
; it is there, handle condition
Endif
Re: What is the fastest way of cheking if a file exists?
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
- travismcgee
- New User
- Posts: 9
- Joined: Mon May 29, 2006 2:16 am
-
- PureBasic Expert
- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
Windows only but fast.
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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Code: Select all
Import "ntdll.lib"
RtlDoesFileExists_U(file.p-unicode)
EndImport
Debug RtlDoesFileExists_U("c:\your.file")

["1:0>1"]
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Actually it's not documented anywhere. Only found 591 sites on Google wich is very rare and the over the half is in related to ReactOS and Wine.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Maybe this is fastest?
Code: Select all
Debug PathFileExists_("c:\program files\internet explorer\iexplore.exe")
Debug PathFileExists_("c:\program files\internet explorer\iexplore.exee")
I wrote and have used this for many years and it has never thrown a wobbly... Windoze only.
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