15 Responses to “How to count files in a folder with PowerShell, CMD, or File Explorer”

  • Adrian Santangelo says:

    There’s a much simpler way to do this from a CMD prompt!

    Current directory only, not including subdirectories: dir
    Current directory AND subdirectories (no hidden/system): dir /s
    Current directory AND subdirectories, only hidden/system: dir /s /ah

    Just let it finish running through things, then at the very end will be the total files and directories (as well as their size.)

  • Nhan Nguyen says:

    Hi, thank you for this article!

    Is there a way to use the PowerShell or CMD recursively output file count by file extension? Like: 300 .html, 20 .css, 50 .pdf, etc.?

  • Peter De Backere says:

    VBA – excel : fill in sheet1 # from e.g. folder = “Program Files”
    ‘ CountFiles Macro
    ”Module level variable
    Dim iFilesCount As Integer

    ‘VBA Procedure to Count Files in Folder and Subfolders
    Sub VBAF1_Count_Files_in_Folder_and_Subfolders()

    ‘Variable declaration
    Dim sFldPath As String

    ‘Define Root Folder Path
    sFldPath = “C:Program Files”

    ‘Çall SubProcedure
    Call VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFldPath)

    End Sub

    ‘VBA SubProcedure(Recursive Method) to Count Files in Folder and Subfolders
    Sub VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFolderPath As String)

    ‘Variable declaration
    Dim sFileName As String, oFile As Object
    Dim oFolder As Object ‘Folder
    Dim iFoldersCount As Integer, iFilesCnt As Integer
    Dim iAddFilesCount As Integer
    Dim iFilesCount As Long

    ‘Check for slash
    If Right(sFolderPath, 1) <> “” Then sFolderPath = sFolderPath & “”

    ‘Create FSO Object
    Set oFSO = CreateObject(“Scripting.FileSystemObject”)
    Set oFolder = oFSO.GetFolder(sFolderPath)

    ‘Check Specified Folder exists or not
    If oFSO.FolderExists(sFolderPath) Then
    iFilesCnt = oFolder.Files.Count
    iFilesCount = iFilesCount + iFilesCnt
    End If

    ‘Loop through all subfolders
    indexLine = 0
    For Each oSubfolder In oFolder.SubFolders
    ‘Çheck available files count in a folder
    ‘ If no privileges skip
    On Error Resume Next
    iAddFilesCount = oSubfolder.Files.Count
    ‘Çheck available subfolders count in a folder
    iFoldersCount = oSubfolder.SubFolders.Count
    ‘Recursive Method
    ‘Check if any other subfolders within subfolders
    If iFoldersCount <> 0 Then
    Call VBAF1_SubProcedure_To_Count_Files_in_Folder_and_Subfolders(sFolderPath & oSubfolder.Name)
    indexLine = indexLine + 1
    Worksheets(1).Range(“A” & indexLine) = oSubfolder
    Worksheets(1).Range(“B” & indexLine) = iAddFilesCount
    Else
    ‘Add files count
    iFilesCount = iFilesCount + iAddFilesCount
    ‘If iAddFilesCount = 0 Then
    indexLine = indexLine + 1
    Worksheets(1).Range(“A” & indexLine) = oSubfolder
    Worksheets(1).Range(“B” & indexLine) = iAddFilesCount
    ‘End If

    End If
    Next

    End Sub

  • Maria Rebuzzi says:

    how do I enhance this CMD to include the Modified date

  • vaishnavi2601 says:

    If you want to print the name of folder and number of files in it

    for f in *; do var=`find “$f” | wc -l`; echo “$f: $var”; done

  • l s says:

    (gci -recurse -file | Measure-Object).count

  • danna says:

    my problem is, numbers don’t match. it says 10 files, but opening folder manually there are only 3. anyone?

  • jp says:

    how about showing the number next to the file?

  • Tobi says:

    Hi there If I use the mthode with Powershell i get a different result as the one which is in the file properties… how is that?

  • Niels Grove-Rasmussen says:

    Nice cover of the various tech to count files and folders.
    But it looks like PowerShell get lower numbers than Windows Explorer and Windows Shell Dir. Have you compared the results in detail?

  • Logan says:

    for /f “tokens=1 delims= ” %a in (‘dir C:test ^|findstr “File(s)”‘) do @echo %a
    for /f “tokens=1 delims= ” %a in (‘dir C:Test ^|findstr “Folder(s)”‘) do @echo %a

  • R Dunn says:

    Looking for a way to add a column to a multi-folder view which lists an item count for each folder in the view… so I don’t have to tediously use these methods on individual folders. I see there’s a column which can be added to File Explorer, called “File count”. After it’s added, it remains blank and I don’t know what its actual use is, or how to populate it if it indeed counts the contents of folders.
    For example, in File Explorer, click Pictures… noting that there are several sub-folders listed, there is no column to show the number of items in each sub-folder shown… there is only the total number of items in Pictures (files and folders) shown in the lower left corner.
    If you could suggest a method or a different file manager program that can show item counts of each folder in a multi-folder listing… that would be handy.
    Thanks.

    • Logan says:

      This batch file will help you get a list of the contents and number of files in pictures folder:

      @echo off

      call :EnumFolder %userprofile%pictures
      goto End

      :EnumFolder %1
      echo %1
      for /d %%d in (%1*.*) do call :EnumFolder %%d
      for /f “tokens=1 delims= ” %%a in (‘dir “%1” ^|findstr “File(s)”‘) do @echo %1 : %%a
      exit /b

      :End

  • Jamin Szczesny says:

    Thank’s for your help – though I ultimately I went with…
    @(Get-ChildItem “C:”).count
    Where using the @ forces the result into an array (for accurate counting) and can actually be written using any Get-ChildItem alias (dir, ls, gci) – so for files only (in chosen directory only)…
    @(dir “C:*.*”).count
    Or for files and folders (in chose directory only)
    @(dir “C:*”).count
    Or you can use any combination of the -Recurse -File -Directory you need as well..
    @(dir “C:” -Recurse -File).count
    Note that the -Recurse method seems to take a long time when it’s looking for a lot of files or subfolders so keep this in mind.

Leave a Reply