windows - cmd 和感叹号

标签 windows batch-file cmd delayedvariableexpansion

对于包含感叹号的文件夹名称,以下代码失败。我想我需要启用 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

最佳答案

我看到了 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

关于windows - cmd 和感叹号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234457/

相关文章:

powershell - 使用来自 cmd.exe 的多行 PowerShell 命令

c++ - Windows 服务状态 C++?

powershell - Bat脚本切断下一行的随机字符

windows - Windows 批处理反引号中的管道

windows - %~ 在 REM 语句中

java - 如何使 exe 文件在命令行上写入文本?

batch-file - 命令提示符中的代码在批处理文件中不起作用

windows - 删除名称中包含 "EXAMPLE"的打印机

windows - 对于循环开关处理

c++ - 渲染各向异性纹理的更快方法?