从批处理文件计算每个目录级别的文件和目录

问题描述 投票:3回答:2

我已成功使用批处理文件,该文件计算根目录中的文件和目录总数。

目录结构:

directory structure

这是当前脚本:(获取将子文件夹返回到第n个子文件的文件和文件夹的数量)。

@echo off
set "drive=D:\Download\app"
for /d %%r in ("%drive%\*") do (
    echo Path: %%~fr
    for /F "tokens=1,2,3 delims= " %%i in ('dir/a/s %%~fr ^| find /i "bytes"') do if "%%j"=="File(s)" (
        set numfiles=%%i
    )ELSE (
        for /f %%a in ('dir /b /s /ad %%~fr ^|find /c /v "" ') do set numfolders=%%a)
    echo Files: %numfiles%
    echo Folds: %numfolders%
)

首先,程序输出根目录中的文件总数和文件夹总数,然后转到第一个子文件夹并输出相同的整个树,然后移动到该级别的下一个文件夹等。

编辑

我已经完成了它转到1级子文件夹的部分,并获得了文件和文件夹的总数,但我希望它达到N个子文件夹,这意味着它应该输出根目录中每个文件夹的总数。

这是扩展代码。

@echo off
setLocal EnableDelayedExpansion 
set "drive=C:\Users\%USERNAME%\Downloads\Sandukchi"
set numfiles=
set numfolders=
set count=0;
for /d %%r in ("%drive%\*") do (
    echo %%r
    SET /A count=count + 1
    for /d %%a in ("%%r\*") do set modifiedDate=%%~ta
    for /F "tokens=1,2,3 delims= " %%i in ('dir/a/s "%%r\*" ^| find /i "File(s)"') do set fileSizeBytes=%%k
    for %%* in ("%%r") do set folderName=%%~nx*
    for /F "tokens=1,2,3 delims= " %%i in ('dir/a/s "%%r\*" ^| find /i "bytes"') do if "%%j"=="File(s)" (
        set numfiles=%%i
    )ELSE (
        for /f %%a in ('dir /b /s /ad "%%r\*" ^|find /c /v "" ') do set numfolders=%%a)
    echo Last Modified Date: !modifiedDate!
    echo Folder Size: !fileSizeBytes! KB
    echo Total Number of Files: !numfiles!
    echo Total Number of Folders: !numfolders!
    (
        echo !count!    %%r     !folderName!        !modifiedDate!  Total Size  !fileSizeBytes!KB   Total Files !numfiles!  Total Folder  !numfolders! 
        echo.
    )>>output.txt
)
windows batch-file
2个回答
1
投票
@ECHO Off
SETLOCAL
SET "sourcedir=."
SET "tempfile=%temp%\##__##.txt"
SET "dirname="
(
FOR /f "tokens=1,2,*delims= " %%w IN (
 'dir /s "%sourcedir%\*" '
 ) DO (
 IF "%%w"=="Directory" (
  SET "dirname=%sp256%%%y"&SET /a fcnt=0&SET /a dcnt=-2
 ) ELSE (
  FOR /f "delims= " %%p IN ("%%y") DO (
   IF "%%p"=="<DIR>" SET /a dcnt+=1
  )
 )
 IF "%%x"=="File(s)" CALL ECHO %%dirname%%*%%w*%%dcnt%%
)
)>"%tempfile%"
FOR /f "tokens=1,2,*delims=*" %%a IN ('sort "%tempfile%"') DO ECHO directory %%a&ECHO     files %%b&echo   subdirs %%c

GOTO :EOF

您需要更改sourcedir的设置以适合您的具体情况。

生成临时文件。没有尝试删除临时文件,因为它可能包含有用的数据。

运行标准的dir/s命令,并选择启动directory(表示新目录名)的行以及第三个以空格分隔的标记为<DIR>来计算子目录的行。当出现带有第二个标记File(s)的行时,将名称,filecount和directorycount输出到tempfile。

对临时文件和报告进行排序。

注意:%%y包含从每一行开始的第三个标记。这被重新标记,仅选择第一个标记(默认)为%%p隔离原始行的第三个标记。

由于*不是有效的文件名字符,因此使用*作为分隔符生成临时文件。

dcnt设置为-2以启动目录计数,因为...都被报告为dir /s中的目录名。


1
投票

尝试使用此代码:

@echo off
Setlocal EnableDelayedExpansion
@For /D  %%D in (*) DO (
    Set "Folder=%%~D"
    PUSHD "!Folder!"
        FOR /F %%H in ('dir /a-d /b 2^>NUL^|find /C /V "" ') DO ( Set "numFiles=%%H" )
        FOR /F %%I in ('dir /ad  /b 2^>NUL^|find /C /V "" ') DO ( Set "numSubFolders=%%I" )
    POPD
    echo The Folder "!Folder!" has !numSubFolders! SubFolders and !numFiles! Files      
    )
)
pause & exit
© www.soinside.com 2019 - 2024. All rights reserved.