还报告了目录中的文件数以及子目录中的文件数 [英] Count of Files in a Directory With Count of Files in Sub Directories Also Reported

查看:71
本文介绍了还报告了目录中的文件数以及子目录中的文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望DOS命令在给定目录中查找文件计数(该目录下的子目录的计数应具有单独的条目)。

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).

对于例如,如果Tech是一个目录,并且具有2个文件和2个子目录(Info Tech,服务),并且子目录每个都有2个文件,则我的结果应类似于

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...

推荐答案

您的要求不清楚。

我假设对于每个文件夹,您都希望该文件夹中的文件总数,包括子文件夹中的文件(递归) )。您想对根文件夹以及所有子文件夹(递归)进行计算。

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).

此代码适用于XP以上版本的所有Windows版本。我不确定它是否适用于真正的MS-DOS。您是否真的在使用MS-DOS?

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

该代码从下至上列出了文件夹,文件计数。如果要自上而下列出文件夹,则只需对结果进行排序。假设批处理脚本的名称为fileCnt.bat,则

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

这篇关于还报告了目录中的文件数以及子目录中的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆