Autohotkey Count Number of Files/Folders in a Directory

What?
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:
copyraw
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
  1.  UserFolder:="C:" 
  2.  -- UserFolder := RegExReplace( MyInputField, "\\$")  ; gets rid of trailing slash if required 
  3.   
  4.  -- Method #1 
  5.  count :0 
  6.  Loop, %UserFolder%\*.*, 0, 1 
  7.    count++ 
  8.   
  9.  -- note for future use: 
  10.  ; if A_LoopFileAttrib contains H,R,
  11.  ;    continue 
This works fine at home on your local host on a local drive. Try using this over a networked drive and more time will be spent counting the files then the actual processing (or whatever your script is trying to do).

Method #2: FileSystemObject ComObjCreate
copyraw
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
}
  1.  UserFolder:="C:" 
  2.   
  3.  -- to return file count in a folder 
  4.  ItemCount :ComObjCreate("Shell.Application").NameSpace(UserFolder).Items.Count 
  5.  FileCount :ComObjCreate("Scripting.FileSystemObject").GetFolder(UserFolder).Files.Count 
  6.  FolderCount := ItemCount - FileCount 
  7.   
  8.   
  9.  -- quick if looking at one directory. 
  10.  -- just as slow if looping for subdirectories: 
  11.   
  12.  Loop, %UserFolder%\*.*, 1, 1 
  13.  { 
  14.          ThisFolder :RegExReplace( A_LoopFileFullPath, "\\$" ) 
  15.          ItemCount :ComObjCreate("Shell.Application").NameSpace(ThisFolder).Items.Count 
  16.          FileCount :ComObjCreate("Scripting.FileSystemObject").GetFolder(ThisFolder).Files.Count 
  17.          FolderCount := ItemCount - FileCount 
  18.          TotalFileCount += FileCount 
  19.          TotalFolderCount += FolderCount 
  20.  } 

Method #3: Standard Function with Loop
copyraw
-- 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.
  1.  -- input parameters: 
  2.  -- -- Folder=folder/file pattern. 
  3.  -- -- Subfolders=1 or 0 (recurse into subfolders or not) 
  4.   
  5.  SC_Load(Folder, Subfolders){ 
  6.          global SC_Size,SC_Ext,SC_FullPath,SC_Name,SC_TotalFiles 
  7.          SC_Size         :Object() 
  8.          SC_Ext          :Object() 
  9.          SC_FullPath     :Object() 
  10.          SC_Name         :Object() 
  11.          SC_TotalFiles   :0 
  12.   
  13.          Loop, %Folder%, , %Subfolders% 
  14.          { 
  15.                  SC_Size[A_Index]        := A_LoopFileSize 
  16.                  SC_Ext[A_Index]         := A_LoopFileExt 
  17.                  SC_FullPath[A_Index]    := A_LoopFileFullPath 
  18.                  SC_Name[A_Index]        := A_LoopFileName 
  19.                  SC_TotalFiles           := A_Index 
  20.          } 
  21.  } 
  22.   
  23.  -- put this function at the end of the script. 
  24.   
  25.  -- usage: 
  26.  SC_Load("C:\*.*", 1) 
  27.  MsgBox, TotalFiles %SC_TotalFiles% 
  28.   
  29.  -- yields number of files in a directory recursively. 

Method #4: FileSystemObject ComObjCreate
copyraw
-- 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
}
  1.  -- usage (see functions below) 
  2.  TotalFileCount :CountFilesRecursive( "C:\" ) 
  3.  MsgBox, TotalFiles %TotalFileCount% 
  4.   
  5.   
  6.  -- functions to put at bottom of script 
  7.  CountFilesRecursive(Folder){ 
  8.          static Counter=0,  fso 
  9.          fso := fso?fso:ComObjCreate("Scripting.FileSystemObject") 
  10.          Folder := fso.GetFolder(Folder) , Counter += Counter?0:CountFiles(Folder.path) 
  11.          For Subfolder in Folder.SubFolders 
  12.                  Counter +CountFiles(Subfolder.path) , CountFilesRecursive(Subfolder.path) 
  13.          return Counter 
  14.  } 
  15.   
  16.  CountFiles(Folder) { 
  17.          fso :ComObjCreate("Scripting.FileSystemObject") 
  18.          Folder := fso.GetFolder(Folder) 
  19.          return fso.GetFolder(Folder).Files.Count 
  20.  } 
Method #5: MS-DOS Command Prompt
copyraw
dir c:\*.* /os /s

-- bear with me as i try to create a for loop one to omit all the header info
  1.  dir c:\*.* /os /s 
  2.   
  3.  -- bear with me as i try to create a for loop one to omit all the header info 


Category: AutoHotkey :: Article: 512

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.