在批处理变量中逃离&符号

时间:2016-09-29 13:40:24

标签: batch-file escaping ampersand

我试图整理一个复杂的批处理脚本,它可以同时执行多项操作。我挂了它的一部分。 我希望脚本询问客户编号,然后将该编号放在URL中的特定位置。 问题是URL有一个&符号,当我回显变量内容时,它会出错。

@echo off
set /p id="Enter ID: "
set URL=https://firsturlsectionhere.com
set FULLURL=%URL%id%^&therestofurl
@echo %FULLURL%

你们给予的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

如果使用引号设置变量,则无需转义&符号。但是因为你有&符号,你需要通过延迟扩展来回显变量。

 @echo off
 set /p id="Enter ID: "
 set URL=https://firsturlsectionhere.com
 set "FULLURL=%URL%/%id%&therestofurl"
 setlocal enabledelayedexpansion
 echo !FULLURL!
 endlocal
 pause

如果你将变量中的转义字符和引号括在set命令中,那么你可以按原样回显该变量。

 @echo off
 set /p id="Enter ID: "
 set URL=https://firsturlsectionhere.com
 set "FULLURL=%URL%/%id%^&therestofurl"
 echo %FULLURL%
 pause