So this is an article to list methods of retrieving the number of files in a folder/directory.
Why?
Why can't we just use a loop and file pattern native to the Autohotkey programming language:
UserFolder:="C:" -- UserFolder := RegExReplace( MyInputField, "\\$") ; gets rid of trailing slash if required -- Method #1 count := 0 Loop, %UserFolder%\*.*, 0, 1 count++ -- note for future use: ; if A_LoopFileAttrib contains H,R,S ; continue
- UserFolder:="C:"
- -- UserFolder := RegExReplace( MyInputField, "\\$") ; gets rid of trailing slash if required
- -- Method #1
- count := 0
- Loop, %UserFolder%\*.*, 0, 1
- count++
- -- note for future use:
- ; if A_LoopFileAttrib contains H,R,S
- ; continue
Method #2: FileSystemObject ComObjCreate
UserFolder:="C:" -- to return file count in a folder ItemCount := ComObjCreate("Shell.Application").NameSpace(UserFolder).Items.Count FileCount := ComObjCreate("Scripting.FileSystemObject").GetFolder(UserFolder).Files.Count FolderCount := ItemCount - FileCount -- quick if looking at one directory. -- just as slow if looping for subdirectories: Loop, %UserFolder%\*.*, 1, 1 { ThisFolder := RegExReplace( A_LoopFileFullPath, "\\$" ) ItemCount := ComObjCreate("Shell.Application").NameSpace(ThisFolder).Items.Count FileCount := ComObjCreate("Scripting.FileSystemObject").GetFolder(ThisFolder).Files.Count FolderCount := ItemCount - FileCount TotalFileCount += FileCount TotalFolderCount += FolderCount }
- UserFolder:="C:"
- -- to return file count in a folder
- ItemCount := ComObjCreate("Shell.Application").NameSpace(UserFolder).Items.Count
- FileCount := ComObjCreate("Scripting.FileSystemObject").GetFolder(UserFolder).Files.Count
- FolderCount := ItemCount - FileCount
- -- quick if looking at one directory.
- -- just as slow if looping for subdirectories:
- Loop, %UserFolder%\*.*, 1, 1
- {
- ThisFolder := RegExReplace( A_LoopFileFullPath, "\\$" )
- ItemCount := ComObjCreate("Shell.Application").NameSpace(ThisFolder).Items.Count
- FileCount := ComObjCreate("Scripting.FileSystemObject").GetFolder(ThisFolder).Files.Count
- FolderCount := ItemCount - FileCount
- TotalFileCount += FileCount
- TotalFolderCount += FolderCount
- }
Method #3: Standard Function with Loop
-- input parameters: -- -- Folder=folder/file pattern. -- -- Subfolders=1 or 0 (recurse into subfolders or not) SC_Load(Folder, Subfolders){ global SC_Size,SC_Ext,SC_FullPath,SC_Name,SC_TotalFiles SC_Size := Object() SC_Ext := Object() SC_FullPath := Object() SC_Name := Object() SC_TotalFiles := 0 Loop, %Folder%, , %Subfolders% { SC_Size[A_Index] := A_LoopFileSize SC_Ext[A_Index] := A_LoopFileExt SC_FullPath[A_Index] := A_LoopFileFullPath SC_Name[A_Index] := A_LoopFileName SC_TotalFiles := A_Index } } -- put this function at the end of the script. -- usage: SC_Load("C:\*.*", 1) MsgBox, TotalFiles %SC_TotalFiles% -- yields number of files in a directory recursively.
- -- input parameters:
- -- -- Folder=folder/file pattern.
- -- -- Subfolders=1 or 0 (recurse into subfolders or not)
- SC_Load(Folder, Subfolders){
- global SC_Size,SC_Ext,SC_FullPath,SC_Name,SC_TotalFiles
- SC_Size := Object()
- SC_Ext := Object()
- SC_FullPath := Object()
- SC_Name := Object()
- SC_TotalFiles := 0
- Loop, %Folder%, , %Subfolders%
- {
- SC_Size[A_Index] := A_LoopFileSize
- SC_Ext[A_Index] := A_LoopFileExt
- SC_FullPath[A_Index] := A_LoopFileFullPath
- SC_Name[A_Index] := A_LoopFileName
- SC_TotalFiles := A_Index
- }
- }
- -- put this function at the end of the script.
- -- usage:
- SC_Load("C:\*.*", 1)
- MsgBox, TotalFiles %SC_TotalFiles%
- -- yields number of files in a directory recursively.
Method #4: FileSystemObject ComObjCreate
-- usage (see functions below) TotalFileCount := CountFilesRecursive( "C:\" ) MsgBox, TotalFiles %TotalFileCount% -- functions to put at bottom of script CountFilesRecursive(Folder){ static Counter=0, fso fso := fso?fso:ComObjCreate("Scripting.FileSystemObject") Folder := fso.GetFolder(Folder) , Counter += Counter?0:CountFiles(Folder.path) For Subfolder in Folder.SubFolders Counter += CountFiles(Subfolder.path) , CountFilesRecursive(Subfolder.path) return Counter } CountFiles(Folder) { fso := ComObjCreate("Scripting.FileSystemObject") Folder := fso.GetFolder(Folder) return fso.GetFolder(Folder).Files.Count }
- -- usage (see functions below)
- TotalFileCount := CountFilesRecursive( "C:\" )
- MsgBox, TotalFiles %TotalFileCount%
- -- functions to put at bottom of script
- CountFilesRecursive(Folder){
- static Counter=0, fso
- fso := fso?fso:ComObjCreate("Scripting.FileSystemObject")
- Folder := fso.GetFolder(Folder) , Counter += Counter?0:CountFiles(Folder.path)
- For Subfolder in Folder.SubFolders
- Counter += CountFiles(Subfolder.path) , CountFilesRecursive(Subfolder.path)
- return Counter
- }
- CountFiles(Folder) {
- fso := ComObjCreate("Scripting.FileSystemObject")
- Folder := fso.GetFolder(Folder)
- return fso.GetFolder(Folder).Files.Count
- }
dir c:\*.* /os /s -- bear with me as i try to create a for loop one to omit all the header info
- dir c:\*.* /os /s
- -- bear with me as i try to create a for loop one to omit all the header info