stanigator
stanigator

Reputation: 10934

What's the easiest way to find out the number of files in a directory of folders?

Is this easily achievable with MS-DOS scripts, or would I have to resort to C++, Python, Perl, or other ways to achieve this?

Upvotes: 1

Views: 5143

Answers (3)

Bas Bossink
Bas Bossink

Reputation: 9678

in cmd you can do:

set i=0
for /f %%G in ('dir /b /A-d <dirname>') do set /a i=i+1

in PowerShell (which is available by default in Windows 7) you can do:

Get-ChildItem <dirname> | Where-Object { -not $_.PSIsContainer } | Measure-Object

These commands count only files if you want to count directories as well remove /A-d for the dos version and Where-Object { -not $_PSIsContainer } for the powershell command. If you want to recurse into the subdirectories use Get-ChildItem -Recurse in powershell or add a /s switch to the dir command in the dos version

Upvotes: 1

mikerobi
mikerobi

Reputation: 20878

To be easiest, it has to pass the "my grandmother can do it" benchmark.

After thorough benchmarking, I determined that the easiest approach would be to open each folder in windows explorer, look at the file count in the status bar, and use a calculator to add the counts.

It is slow, but effective.

EDIT

It might not be as easy, but as a heavy cygwin user, I tend to use the find command and wc.

find -type 'f' | wc -l

Upvotes: 3

verdesmarald
verdesmarald

Reputation: 11866

This is very straightforward in python:

import os

walker = os.walk(path_to_search_root)
files = 0

for dir in walker:
    files += len(dir[2]) #dir[2] is the list of files in the directory

print files

Upvotes: 0

Related Questions