感叹号(!)从CMD%CD%输出中删除

Windows CMDshell正在剥离目录名称中的“ ! ”字符。

假设当前的工作目录是“ C:\\!MyFolder

在.CMD文件中我使用这个语法:

 set _STARTPATH=%CD% echo %_STARTPATH% 

C:\MyFolder显示时没有爆炸( !

从WinXP到Win8.1这是常见的。

问:有没有人知道这个解决办法?

当你启用延迟扩展时,你也应该把它用于变量扩展

 set _STARTPATH=!CD! echo !_STARTPATH! 

delayedexpansion你可以尝试变量搜索/替换 逃脱 !

 echo %cd:!=^^!% 

但解决办法是暂时禁用 delayed expansion

 ... setlocal disabledelayedexpansion echo %cd% setlocal enabledelayedexpansion ... 

 @echo off setlocal rd !MyFolder md !MyFolder setlocal enabledelayedexpansion echo enabledelayedexpansion is ON @timeout /t 1 /nobreak>nul echo dir /b /ad "^!*" echo. dir /b /ad "^!*" @timeout /t 1 /nobreak>nul echo. echo cd ^^^^^^^^^^^!Myfolder 7 more caret to show double caret ecaping ^^! in echo - 13 carets used in that line. echo cd ^^!Myfolder double caret to escape ^^! echo cd "^!Myfolder" single caret inside double quotes to escape ^^! cd ^^!Myfolder @timeout /t 1 /nobreak>nul echo. echo ^^^!cd^^^!: !cd! echo %%cd%%: %cd:!=^^!% @timeout /t 1 /nobreak>nul endlocal echo. echo %%cd%% and ^!cd^! after endlocal: @timeout /t 1 /nobreak>nul echo %cd% echo !cd! exit /b 0