1

Windows CMD shell 正在!从目录名称中去除“”字符。

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

在 .CMD 文件中,我使用以下语法:

set _STARTPATH=%CD%
echo %_STARTPATH%

C:\MyFolder显示时不带 bang ( !)

这在 WinXP 到 Win8.1 中很常见。

问:有人知道解决这个问题的方法吗?

4

2 回答 2

2

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

set _STARTPATH=!CD!
echo !_STARTPATH!
于 2015-10-03T20:50:56.297 回答
1

您可以通过转义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
于 2015-10-03T15:37:30.727 回答