A directory is a container that is used to contain subdirectories and files. Sometimes, may need know how many files are stored inside a given directory and subdirectories. This tutorial shows how to count files in directory on Windows.
Method 1 - CMD
To count files recursively in directory, use the dir command for finding files and find command to count the number of files.
dir /a:-d /s /b "C:\Windows\System32\drivers" | find /c /v ""
The meaning of parameters that used in dir command:
/a:-d- find only files, exclude directories./s- find all files in the specified directory and all subdirectories./b- display a list of files without additional information.
Combination of parameters /c and /v in the find command means that need to display a count of the lines that don't contain the specified string. In our case empty string "".
In order to count files in the first-level directory, remove /s parameter for dir command.
dir /a:-d /b "C:\Windows\System32\drivers" | find /c /v ""
Method 2 - PowerShell
Use Get-ChildItem command in PowerShell to count files recursively in directory:
Get-ChildItem "C:\Windows\System32\drivers" -Force -Recurse -File -ErrorAction Ignore | Measure-Object | %{$_.Count}
Remove -Recurse parameter in order to count files in the first-level directory:
Get-ChildItem "C:\Windows\System32\drivers" -Force -File -ErrorAction Ignore | Measure-Object | %{$_.Count}
Leave a Comment
Cancel reply