简体   繁体   English

将带有'&'的路径传递给批处理文件

[英]Passing a Path with '&' to Batch File

I am trying to pass a path to a batch file from another program. 我试图从另一个程序传递一个批处理文件的路径。 The other program uses BASIC as it's scripting language. 另一个程序使用BASIC作为脚本语言。

So, I am calling the batch file via the Shell() command. 所以,我通过Shell()命令调用批处理文件。

In the shell command, I am trying to pass in a path that has the symbol '&'. 在shell命令中,我试图传入一个带有符号'&'的路径。 However, by the time it gets to the batch file, the batch file treats the & as sort of an escape sequence (I think). 但是,当它到达批处理文件时,批处理文件将&视为一种转义序列(我认为)。

For example: 例如:

Path: C:\\Documents\\R&D\\Files 路径:C:\\ Documents \\ R&D \\ Files

Shell("runscript.bat "C:\Documents\R&D\Files"")

In the batch file: 在批处理文件中:

variable=~1
echo %variable%

I am getting the following output: 我得到以下输出:

C:Documents\\R C:文档\\ r

UPDATE UPDATE

However, if I use a path like C:\\Documents\\RD\\Files 但是,如果我使用像C:\\ Documents \\ RD \\ Files这样的路径

This path will come through find and the echo will print: 此路径将通过查找并且回显将打印:

C:\\Documents\\RD\\Files C:\\文档\\ RD \\文件

I suppose you want to use the variable without quotes.(Enclosing it in quotes will cause you no problems).Then you need delayed expansion (and accessing variable with ! instead of % ): 我想你想要使用没有引号的变量。(将它括在引号中会导致你没有问题。)然后你需要延迟扩展(并使用!而不是%来访问变量):

set "variable=%~1"
echo "%variable%"
setlocal enableDelayedExpansion
echo !variable!

Here I'm assuming the first argument is something contains & 在这里,我假设第一个参数是包含&

Another option is for /f : 另一种选择是for /f

for /f %%a in ("%~1") do echo %%a

Windows allows you to use ^ to escape the next character in a string. Windows允许您使用^来转义字符串中的下一个字符。 Try R^&D as shown below: 尝试R^&D ,如下所示:

C:\Users\Thomas>echo R^&D
R&D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM