Quickest way to find if a file exist in win OS
Quickest way to find if a file exist in win OS
Hi,
I am looking for the quickest way to check if a file exists. I search the forum and find 2 methods:
1) If FileSize(FileName)=-1 ---> file not found
2) if Not(GetFileAttributes(FileName) & #FILE_ATTRIBUTE_DIRECTORY = #False) ---> file not found
Any other suggestions?
Thanks.
Allen
I am looking for the quickest way to check if a file exists. I search the forum and find 2 methods:
1) If FileSize(FileName)=-1 ---> file not found
2) if Not(GetFileAttributes(FileName) & #FILE_ATTRIBUTE_DIRECTORY = #False) ---> file not found
Any other suggestions?
Thanks.
Allen
Re: Quickest way to find if a file exist in win OS
On windows, there is a api function available with Purebasic like this:
BTW: I do not understand why you need the quickest way for that?
Code: Select all
Global file$ = "E:\Temp\Source\ExistingFile.txt" ; <-- change to something usable
If PathFileExists_(@file$)
Debug "YES "
Else
Debug "NO ErrorCode = 0x" + Hex(GetLastError_())
EndIf
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Re: Quickest way to find if a file exist in win OS
Axolotl,
Thanks for the information.
I wrote a backup program for my own use and I currently use filesize to check if a file is already backup or not. I am trying to improve the backup speed as there are around 400000 files in the backup drive.
I am also considering to use CRC32 or MD5 to create a database of my already backup files to speed up the purpose. To handle updated files, the backuped files name will contain the file modification/creation date. To check if I need to backup D:\User\Phone.txt that has modification date 2022_0813_121323, I shall check if D\User\Phone_2022_0813_121323.txt exist in my backup drive, if not I shall copy it.
Allen
Thanks for the information.
I wrote a backup program for my own use and I currently use filesize to check if a file is already backup or not. I am trying to improve the backup speed as there are around 400000 files in the backup drive.
I am also considering to use CRC32 or MD5 to create a database of my already backup files to speed up the purpose. To handle updated files, the backuped files name will contain the file modification/creation date. To check if I need to backup D:\User\Phone.txt that has modification date 2022_0813_121323, I shall check if D\User\Phone_2022_0813_121323.txt exist in my backup drive, if not I shall copy it.
Allen
Re: Quickest way to find if a file exist in win OS
Allen,
okay, I see.
Some thoughs:
If you do not (never) touch your backup files accidently, you may use the archive flag as an indicator (on windows)
By use of there is to get the flags.
Otherwise I would compare the modification dates only.
and if they are different ... (you know what to do)
Computing a checksum are much more time consuming than check the existance of a file (i guess).
BTW: There are several tools around. Even some codes about creating lists of files and compare files and so on.
But (on the other hand) I do understand: Where is the fun in using other people's ready made programs.
Happy coding and stay healthy.
okay, I see.
Some thoughs:
If you do not (never) touch your backup files accidently, you may use the archive flag as an indicator (on windows)
By use of
Code: Select all
ExamineDirectory()
Code: Select all
DirectoryEntryAttributes()
Code: Select all
#PB_FileSystem_Archive ; File has been changed and not archived since the last time
and if they are different ... (you know what to do)
Computing a checksum are much more time consuming than check the existance of a file (i guess).
BTW: There are several tools around. Even some codes about creating lists of files and compare files and so on.
But (on the other hand) I do understand: Where is the fun in using other people's ready made programs.
Happy coding and stay healthy.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Re: Quickest way to find if a file exist in win OS
Axolotl,
Thanks a lot for your information. I definitely will study and learn from them.
For me, the most fun part is to learn new things/technique and use them.
Allen
Thanks a lot for your information. I definitely will study and learn from them.
For me, the most fun part is to learn new things/technique and use them.
Allen
Re: Quickest way to find if a file exist in win OS
This might be useful, use Getfilelist then you can go through and check the attributes
Code: Select all
EnableExplicit
Structure FileDate
Created.i
Modified.i
Accessed.i
EndStructure
Structure File
Name.s
Attributes.i
Date.FileDate
Size.q
EndStructure
Procedure GetFileList(StartDirectory.s,List Lfiles.file(),Pattern.s="*.*",Recursive=1)
Protected PatternCount,Depth,a,CurrentDirectoryID,Directory.s,TempDirectory.s
Protected FileAttributes.i,FileSize.i,FileDate.FileDate,FileName.s,FullFileName.s
Static NewList Lpattern.s()
Static PatternSet,FileCount
If Not PatternSet
Pattern = RemoveString(Pattern,"*.")
PatternCount = CountString(Pattern,"|") + 1
ClearList(lpattern())
For a = 1 To PatternCount
AddElement(Lpattern())
Lpattern() = UCase(StringField(Pattern,a,"|"))
Next
PatternSet=1
ElseIf depth = 0
PatternSet = 0
EndIf
CurrentDirectoryID = ExamineDirectory(#PB_Any, StartDirectory, "*.*")
If CurrentDirectoryID
While NextDirectoryEntry(CurrentDirectoryID)
If DirectoryEntryType(CurrentDirectoryID) = #PB_DirectoryEntry_File
Directory = StartDirectory
FileName = DirectoryEntryName(CurrentDirectoryID)
FileDate\Created = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Created)
FileDate\Modified = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Modified)
FileDate\Accessed = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Accessed)
FileAttributes = DirectoryEntryAttributes(CurrentDirectoryID)
FileSize = DirectoryEntrySize(CurrentDirectoryID)
ForEach Lpattern()
If lpattern() = "*" Or GetExtensionPart(UCase(FileName)) = lpattern()
FullFileName.s = StartDirectory + FileName
AddElement(LFiles())
Lfiles()\Name = FullFileName
Lfiles()\Date = FileDate
Lfiles()\Size = FileSize
Lfiles()\Attributes = FileAttributes
FileCount+1
EndIf
Next
Else
TempDirectory = DirectoryEntryName(CurrentDirectoryID)
If TempDirectory <> "." And TempDirectory <> ".."
If Recursive = 1
Depth + 1
GetFileList(StartDirectory + TempDirectory + #PS$,LFiles(),Pattern,Recursive)
EndIf
EndIf
EndIf
Wend
FinishDirectory(CurrentDirectoryID)
EndIf
ProcedureReturn FileCount
EndProcedure
Procedure SortFileListByDate(List InputFiles.file(),List OutPutFiles.file(),Order=#PB_Sort_Ascending,DateOption=#PB_Date_Modified,StartDate=0,EndDate=$7FFFFFFF)
Select DateOption
Case #PB_Date_Modified
SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Modified)),#PB_Integer)
Case #PB_Date_Accessed
SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Accessed)),#PB_Integer)
Case #PB_Date_Created
SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Created)),#PB_Integer)
EndSelect
If StartDate
ForEach InputFiles()
Select DateOption
Case #PB_Date_Modified
If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Modified <= EndDate)
AddElement(OutPutFiles())
CopyStructure(@InputFiles(),@OutPutFiles(),File)
EndIf
Case #PB_Date_Accessed
If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Accessed <= EndDate)
AddElement(OutPutFiles())
CopyStructure(@InputFiles(),@OutPutFiles(),File)
EndIf
Case #PB_Date_Created
If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Created <= EndDate)
AddElement(OutPutFiles())
CopyStructure(@InputFiles(),@OutPutFiles(),File)
EndIf
EndSelect
Next
EndIf
ProcedureReturn ListSize(OutPutFiles())
EndProcedure
Procedure SortFileListBySize(List InputFiles.file(),List OutPutFiles.file(),Order=#PB_Sort_Ascending,MinimumSize=0,MaximumSize.q=$7FFFFFFFFFFFFFFF)
SortStructuredList(InputFiles(),Order,OffsetOf(File\Size),#PB_Integer)
If MinimumSize
ForEach InputFiles()
If (InputFiles()\Size >= MinimumSize And InputFiles()\Size <= MaximumSize)
AddElement(OutPutFiles())
CopyStructure(@InputFiles(),@OutPutFiles(),File)
EndIf
Next
EndIf
ProcedureReturn ListSize(OutPutFiles())
EndProcedure
Global NewList AllFiles.File()
Global NewList FilteredFiles.File()
Global NewList ReFilteredFiles.File()
Global StartDate = Date(2014,1,1,0,0,0)
Global EndDate = Date(2022,12,31,23,59,59)
Global Path.s = #PB_Compiler_Home
If GetFileList(Path,AllFiles(),"*.pb|*.pbi") ;get all pb files in the directory recursively
If SortFileListByDate(Allfiles(),FilteredFiles(),#PB_Sort_Ascending,#PB_Date_Modified,StartDate,EndDate) ;sort and filter by date between dates
ForEach FilteredFiles()
Debug FilteredFiles()\Name
Debug FormatDate("%dd/%mm/%yyyy",FilteredFiles()\Date\Modified)
Next
EndIf
Debug "++++++++++++++++++++++++++++++++++++++++++++++++++"
If SortFileListBySize(FilteredFiles(),ReFilteredFiles(),#PB_Sort_Descending,10000,100000) ;re-sort and filter by size between sizes
ForEach ReFilteredFiles()
Debug ReFilteredFiles()\Name
Debug Str(ReFilteredFiles()\Size / 1024) + " KB"
Next
EndIf
EndIf
Re: Quickest way to find if a file exist in win OS
@Idle,
Thank you for your example, will study and check if it useful to integrate part of it in my program.
Allen
Thank you for your example, will study and check if it useful to integrate part of it in my program.
Allen
Re: Quickest way to find if a file exist in win OS
Just a reminder: PureBasic has had #PS$ for the path separation character for a while now. You don't need to manually specify it:
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#Cdir = "\"
CompilerElse
#Cdir = "/"
CompilerEndIf
Debug #Cdir
Debug #PS$
Re: Quickest way to find if a file exist in win OS
Thanks for that updated the listing aboveBarryG wrote: ↑Wed Sep 21, 2022 3:06 am Just a reminder: PureBasic has had #PS$ for the path separation character for a while now. You don't need to manually specify it:
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows #Cdir = "\" CompilerElse #Cdir = "/" CompilerEndIf Debug #Cdir Debug #PS$
- Zebuddi123
- Enthusiast
- Posts: 794
- Joined: Wed Feb 01, 2012 3:30 pm
- Location: Nottinghamshire UK
- Contact:
Re: Quickest way to find if a file exist in win OS
Voidtools Everything sdk On windows "The Bee`s Knee`s" https://www.voidtools.com/support/everything/sdk/
Zebuddi.
all procs available in dll + lib
Current Ver:1.4.1.1015
1. Everything_CleanUp
2. Everything_DeleteRunHistory
3. Everything_Exit
4. Everything_GetBuildNumber
5. Everything_GetLastError
6. Everything_GetMajorVersion
7. Everything_GetMatchCase
8. Everything_GetMatchPath
9. Everything_GetMatchWholeWord
10. Everything_GetMax
11. Everything_GetMinorVersion
12. Everything_GetNumFileResults
13. Everything_GetNumFolderResults
14. Everything_GetNumResults
15. Everything_GetOffset
16. Everything_GetRegex
17. Everything_GetReplyID
18. Everything_GetReplyWindow
19. Everything_GetRequestFlags
20. Everything_GetResultAttributes
21. Everything_GetResultDateAccessed
22. Everything_GetResultDateCreated
23. Everything_GetResultDateModified
24. Everything_GetResultDateRecentlyChanged
25. Everything_GetResultDateRun
26. Everything_GetResultExtensionA
27. Everything_GetResultExtensionW
28. Everything_GetResultFileListFileNameA
29. Everything_GetResultFileListFileNameW
30. Everything_GetResultFileNameA
31. Everything_GetResultFileNameW
32. Everything_GetResultFullPathNameA
33. Everything_GetResultFullPathNameW
34. Everything_GetResultHighlightedFileNameA
35. Everything_GetResultHighlightedFileNameW
36. Everything_GetResultHighlightedFullPathAndFileNameA
37. Everything_GetResultHighlightedFullPathAndFileNameW
38. Everything_GetResultHighlightedPathA
39. Everything_GetResultHighlightedPathW
40. Everything_GetResultListRequestFlags
41. Everything_GetResultListSort
42. Everything_GetResultPathA
43. Everything_GetResultPathW
44. Everything_GetResultRunCount
45. Everything_GetResultSize
46. Everything_GetRevision
47. Everything_GetRunCountFromFileNameA
48. Everything_GetRunCountFromFileNameW
49. Everything_GetSearchA
50. Everything_GetSearchW
51. Everything_GetSort
52. Everything_GetTargetMachine
53. Everything_GetTotFileResults
54. Everything_GetTotFolderResults
55. Everything_GetTotResults
56. Everything_IncRunCountFromFileNameA
57. Everything_IncRunCountFromFileNameW
58. Everything_IsAdmin
59. Everything_IsAppData
60. Everything_IsDBLoaded
61. Everything_IsFastSort
62. Everything_IsFileInfoIndexed
63. Everything_IsFileResult
64. Everything_IsFolderResult
65. Everything_IsQueryReply
66. Everything_IsVolumeResult
67. Everything_QueryA
68. Everything_QueryW
69. Everything_RebuildDB
70. Everything_Reset
71. Everything_SaveDB
72. Everything_SaveRunHistory
73. Everything_SetMatchCase
74. Everything_SetMatchPath
75. Everything_SetMatchWholeWord
76. Everything_SetMax
77. Everything_SetOffset
78. Everything_SetRegex
79. Everything_SetReplyID
80. Everything_SetReplyWindow
81. Everything_SetRequestFlags
82. Everything_SetRunCountFromFileNameA
83. Everything_SetRunCountFromFileNameW
84. Everything_SetSearchA
85. Everything_SetSearchW
86. Everything_SetSort
87. Everything_SortResultsByPath
88. Everything_UpdateAllFolderIndexes
Zebuddi.
all procs available in dll + lib
Current Ver:1.4.1.1015
1. Everything_CleanUp
2. Everything_DeleteRunHistory
3. Everything_Exit
4. Everything_GetBuildNumber
5. Everything_GetLastError
6. Everything_GetMajorVersion
7. Everything_GetMatchCase
8. Everything_GetMatchPath
9. Everything_GetMatchWholeWord
10. Everything_GetMax
11. Everything_GetMinorVersion
12. Everything_GetNumFileResults
13. Everything_GetNumFolderResults
14. Everything_GetNumResults
15. Everything_GetOffset
16. Everything_GetRegex
17. Everything_GetReplyID
18. Everything_GetReplyWindow
19. Everything_GetRequestFlags
20. Everything_GetResultAttributes
21. Everything_GetResultDateAccessed
22. Everything_GetResultDateCreated
23. Everything_GetResultDateModified
24. Everything_GetResultDateRecentlyChanged
25. Everything_GetResultDateRun
26. Everything_GetResultExtensionA
27. Everything_GetResultExtensionW
28. Everything_GetResultFileListFileNameA
29. Everything_GetResultFileListFileNameW
30. Everything_GetResultFileNameA
31. Everything_GetResultFileNameW
32. Everything_GetResultFullPathNameA
33. Everything_GetResultFullPathNameW
34. Everything_GetResultHighlightedFileNameA
35. Everything_GetResultHighlightedFileNameW
36. Everything_GetResultHighlightedFullPathAndFileNameA
37. Everything_GetResultHighlightedFullPathAndFileNameW
38. Everything_GetResultHighlightedPathA
39. Everything_GetResultHighlightedPathW
40. Everything_GetResultListRequestFlags
41. Everything_GetResultListSort
42. Everything_GetResultPathA
43. Everything_GetResultPathW
44. Everything_GetResultRunCount
45. Everything_GetResultSize
46. Everything_GetRevision
47. Everything_GetRunCountFromFileNameA
48. Everything_GetRunCountFromFileNameW
49. Everything_GetSearchA
50. Everything_GetSearchW
51. Everything_GetSort
52. Everything_GetTargetMachine
53. Everything_GetTotFileResults
54. Everything_GetTotFolderResults
55. Everything_GetTotResults
56. Everything_IncRunCountFromFileNameA
57. Everything_IncRunCountFromFileNameW
58. Everything_IsAdmin
59. Everything_IsAppData
60. Everything_IsDBLoaded
61. Everything_IsFastSort
62. Everything_IsFileInfoIndexed
63. Everything_IsFileResult
64. Everything_IsFolderResult
65. Everything_IsQueryReply
66. Everything_IsVolumeResult
67. Everything_QueryA
68. Everything_QueryW
69. Everything_RebuildDB
70. Everything_Reset
71. Everything_SaveDB
72. Everything_SaveRunHistory
73. Everything_SetMatchCase
74. Everything_SetMatchPath
75. Everything_SetMatchWholeWord
76. Everything_SetMax
77. Everything_SetOffset
78. Everything_SetRegex
79. Everything_SetReplyID
80. Everything_SetReplyWindow
81. Everything_SetRequestFlags
82. Everything_SetRunCountFromFileNameA
83. Everything_SetRunCountFromFileNameW
84. Everything_SetSearchA
85. Everything_SetSearchW
86. Everything_SetSort
87. Everything_SortResultsByPath
88. Everything_UpdateAllFolderIndexes
malleo, caput, bang. Ego, comprehendunt in tempore
- Zebuddi123
- Enthusiast
- Posts: 794
- Joined: Wed Feb 01, 2012 3:30 pm
- Location: Nottinghamshire UK
- Contact:
Re: Quickest way to find if a file exist in win OS
malleo, caput, bang. Ego, comprehendunt in tempore
Re: Quickest way to find if a file exist in win OS
miskox, Zebuddi123,
Thanks a lot for the information. Definitely will try to test use them.
Allen
Thanks a lot for the information. Definitely will try to test use them.
Allen