我有两个批处理文件(XP):
test.bat:
setlocal EnableDelayedExpansion
rem check for argument
if [%1]==[] goto :noarg
:arg
rem check for trailing backslash and remove it if it's there
set dirname=%1
IF !dirname:~-1!==\ SET "dirname=!dirname:~0,-1!"
rem find all log files in passed directory and call test2.bat for each one
for /f "tokens=* delims= " %%a in ('dir !dirname!\*.log /s /b') do call test2.bat "%%a"
goto :finish
:noarg
rem prompt for directory to scan
set /p dirname=Type the drive or directory, then hit enter:
rem loop if nothing entered
if [!dirname!]==[] goto :noarg
rem check for trailing backslash and remove it if it's there
IF !dirname:~-1!==\ SET "dirname=!dirname:~0,-1!"
rem find all log files in passed directory and call test2.bat for each one
for /f "tokens=* delims= " %%a in ('dir "!dirname!"\*.log /s /b') do call test2.bat "%%a"
goto :finish
:finish
test2.bat:
echo %1
展示问题:
-创建名为c:\test和c:\test!的目录,并在每个目录中放置一个空的test.log文件。
然后运行:
test c:\test
这个测试按预期工作(test2.bat会输出"c:\test\test.log")
现在运行:
test c:\test!
问题在于test2.bat回显了"c:\test\test.log"而不是期望的"c:\test!\test.log"。
我意识到这是因为!被保留用于EnableDelayedExpansion。但如果解决方案是"使用%扩展",那么我就陷入困境了,因为我需要使用DelayedExpansion(根据处理批处理文件中带有空格的尾随反斜杠和目录名)。
我已经进行了以下尝试:
setlocal DisableDelayedExpansion
并且
endlocal
我无法成功地(可能是用户操作问题)转义cmd脚本中的感叹号!。
有什么想法吗?