1

对于包含感叹号的文件夹名称,以下代码将失败。我想我需要启用 DelayedExpansion 来处理嵌套的 for 循环。有什么想法可以完成这项工作吗?谢谢!

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"

set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0

for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
    Set /a Folders+=1
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
        Set NumberOfFiles=%%i
    )
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
        set NumberOfFolders=%%i
    )
    IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
    IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" equ "0" set /a FoldersWithOnlyFiles+=1
    IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithOnlySubFolders+=1
    IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" equ "0" (
        set /a FoldersEmpty+=1
        echo %%a is empty.
    )
)

echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.

endlocal
4

1 回答 1

6

我看到了 3 个简单的解决方案:

1)最慢的方法是使用 CALL ,这样您就不需要在循环中进行延迟扩展。

@echo off & setlocal disableDelayedExpansion

set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"

set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0

for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
    Set /a Folders+=1
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
        Set NumberOfFiles=%%i
    )
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
        set NumberOfFolders=%%i
    )
    call :incrementCounts
)

echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.

endlocal
exit /b

:incrementCounts
IF "%NumberOfFiles%" neq "0" IF "%NumberOfFolders%" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
IF "%NumberOfFiles%" neq "0" IF "%NumberOfFolders%" equ "0" set /a FoldersWithOnlyFiles+=1
IF "%NumberOfFiles%" equ "0" IF "%NumberOfFolders%" neq "0" set /a FoldersWithOnlySubFolders+=1
IF "%NumberOfFiles%" equ "0" IF "%NumberOfFolders%" equ "0" (
    set /a FoldersEmpty+=1
    echo %%a is empty.
)
exit /b

2) 一种更快的方法是暂时启用延迟扩展,仅足够长的时间将值传输到 FOR 变量。

@echo off & setlocal disableDelayedExpansion

set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"

set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0

for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
    Set /a Folders+=1
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
        Set NumberOfFiles=%%i
    )
    for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
        set NumberOfFolders=%%i
    )
    setlocal enableDelayedExpansion
    for /f "tokens=1,2" %%N in ("!NumberOfFiles! !NumberOfFolders!") do (
        endlocal
        IF "%%N" neq "0" IF "%%M" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
        IF "%%N" neq "0" IF "%%M" equ "0" set /a FoldersWithOnlyFiles+=1
        IF "%%N" equ "0" IF "%%M" neq "0" set /a FoldersWithOnlySubFolders+=1
        IF "%%N" equ "0" IF "%%M" equ "0" (
            set /a FoldersEmpty+=1
            echo %%a is empty.
        )
    )
)

echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.

endlocal

3)但我处理它的方式是消除仅在循环中使用的 2 个变量。它们不在循环之外使用,并且它们已经从 FOR 变量派生。由于现在一切都是 FOR 变量,因此您不再需要延迟扩展。

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"

set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0

for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
    Set /a Folders+=1
    for /f "usebackq tokens=*" %%N in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
        for /f "usebackq tokens=*" %%M in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
            IF "%%N" neq "0" IF "%%M" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
            IF "%%N" neq "0" IF "%%M" equ "0" set /a FoldersWithOnlyFiles+=1
            IF "%%N" equ "0" IF "%%M" neq "0" set /a FoldersWithOnlySubFolders+=1
            IF "%%N" equ "0" IF "%%M" equ "0" (
                set /a FoldersEmpty+=1
                echo %%a is empty.
            )
        )
    )
)

echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.

endlocal
于 2013-03-05T21:49:07.437 回答