每个文件夹的文件数和大小

时间:2017-03-06 15:30:40

标签: windows batch-file cmd

我有以下批处理脚本,它给出了目录中每个文件夹的大小。我需要帮助调整或创建一个新脚本,以便它也提供每个文件夹的文件数:

@echo off
    setlocal disabledelayedexpansion

    set "folder=%~1"
    if not defined folder set "folder=%cd%"

    for /d %%a in ("%folder%\*") do (
        set "size=0"
        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"
        setlocal enabledelayedexpansion
        echo(%%~nxa # !size!
        endlocal
    )

    endlocal

2 个答案:

答案 0 :(得分:1)

所有信息都已经在内部dir命令的输出中,您只需要更改要检索的内容

@echo off
    setlocal disabledelayedexpansion

    for %%a in ("%~f1.") do set "folder=%%~fa"

    for /d %%f in ("%folder%\*") do (
        set /a "size=0", "files=0", "directories=0"

        for /f "tokens=1,3,5" %%a in ('
            dir /-c /a /w /s "%%~ff\*" 2^>nul ^| findstr /b /c:"  "
        ') do if "%%~c"=="" ( 
            set "files=%%~a"
            set "size=%%~b"
        ) else set /a "directories=%%~a/3"

        setlocal enabledelayedexpansion
            echo(%%~nxf # !size! bytes : !files! files : !directories! directories
        endlocal
    )

答案 1 :(得分:0)

在最后添加此cmd,它将计算给定目录中的文件

setlocal enableextensions
set count=0
for %%x in ("%folder%\*") do set /a count+=1
echo %count%
endlocal

这是效率低下的,因为您遍历整个目录两次。 理想情况下,您需要将两个FOR循环合并为一个,只需扫描一次dir并将大小添加到大小计数器中,并为每个计数器计算+ = 1。

尝试*添加间距以显示我添加的内容

    set "folder=%~1"
    if not defined folder set "folder=%cd%"

    set count=0

    for /d %%a in ("%folder%\*") do (
        set "size=0"
        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"

        set /a count+=1

        setlocal enabledelayedexpansion
        echo(%%~nxa # !size!
        endlocal
        )

    echo %count%

    endlocal