Count of Files in a Directory With Count of Files in Sub Directories Also Reported

痴心易碎 提交于 2019-12-12 20:41:57

问题


I want the DOS command to find the count of files in a given directory (the count should have separate entries for the sub directories under this directory).

For eg, if Tech is a directory and it has 2 files, and 2 sub-directories (Info Tech, Services), and the sub-directories have 2 files each, the my result should look like

Tech 6
Info Tech 2
Services 2

It would be great if someone could help me with such a command line...


回答1:


Your requirements are not clear.

I'm assuming that for each folder you want the total number of files in the folder, including files in the sub-folders (recursive). You want to do that computation for the root folder, as well as all sub-folders (recursive).

This code works on all versions of Windows from XP on. I'm not sure if it works on true MS-DOS. Are you really using MS-DOS?

@echo off
setlocal disableDelayedExpansion
if "%~1"=="" (call :recurse ".") else call :recurse %1
exit /b

:recurse
setlocal
set fileCnt=0
for /d %%D in ("%~1\*") do call :recurse "%%~fD"
for /f %%A in ('dir /b /a-d "%~1\*" 2^>nul ^| find /v /c ""') do set /a fileCnt+=%%A
echo "%~f1"  %fileCnt%
( 
  endlocal
  set /a fileCnt+=%fileCnt%
)
exit /b

The code lists the folders with file count from the bottom up. If you want the folders listed top down, then simply sort the results. Assuming the batch script is named fileCnt.bat, then

fileCnt | sort


来源:https://stackoverflow.com/questions/10702326/count-of-files-in-a-directory-with-count-of-files-in-sub-directories-also-report

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!