开发者

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

开发者 https://www.devze.com 2023-03-06 06:20 出处:网络
Is this easily achievable with MS-DOS scripts, or would I have to resort to C++, Py开发者_如何学Cthon, Perl, or other ways to achieve this?To be easiest, it has to pass the \"my grandmother can do it\

Is this easily achievable with MS-DOS scripts, or would I have to resort to C++, Py开发者_如何学Cthon, Perl, or other ways to achieve this?


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消