以下代码对包含感叹号的文件夹名称失败。我想我需要启用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
答案 0 :(得分: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