问题描述
下面的批处理文件:
@echo off
set filelocation=C:\Users\myself\Documents\This&That
cd %filelocation%
echo %filelocation%
pause
提供以下输出:
'That' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
C:\Users\myself\Documents\This
Press any key to continue . . .
考虑到我无法更改文件夹名称,如何处理&"
Considering I cannot change the folder name, how do I handle the "&"
推荐答案
您需要进行两项更改.
1)带有引号的扩展集语法在变量名之前和内容的末尾
1) The extended set syntax with quotes prefix the variable name and at the end of the content
2)延迟扩展以安全地使用变量
2) delayed expansion to use the variable in a safe way
setlocal enableDelayedExpansion
set "filelocation=C:\Users\myself\Documents\This&That"
cd !filelocation!
echo !filelocation!
延迟的扩展类似于百分比扩展,但是必须首先使用setlocal EnableDelayedExpansion
启用它,然后可以使用感叹号!variable!
以及仍然使用百分比%variable%
扩展变量.
Delayed expansiuon works like percent expansion, but it have to be enabled first with setlocal EnableDelayedExpansion
and then variables can expanded with exclamation marks !variable!
and still with percents %variable%
.
延迟扩展变量的优点是扩展始终是安全的.
但是就像百分比扩展一样,当您需要将其用作内容时必须将百分比加倍,当您将其用作内容时,也必须使用脱字符号来消除感叹号.
The advantage of the delayed expansion of variables is that the expansion is always safe.
But like the percent expansion, where you have to double percents when you need it as content, you have to escape exclamation marks with a caret when you use it as content.
set "var1=This is a percent %%"
set "var2=This is a percent ^!"
这篇关于批处理文件处理文件夹名称中的&(&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!