简体   繁体   English

Rscript.exe的文件路径中的&符

[英]Ampersand in file path for Rscript.exe

I am trying to run an R script through a batch file that I have written. 我试图通过我编写的批处理文件运行R脚本。 I want the batch script to be flexible so that I can move it to other computers or have it run off of the network drive and it will work in all cases. 我希望批处理脚本具有灵活性,以便可以将其移至其他计算机或使其在网络驱动器上运行,并且在所有情况下均可以使用。 What I do is collect the path of the file where the batch file is opened from, and then use that path to activate the R script. 我要做的是收集从中打开批处理文件的文件的路径,然后使用该路径激活R脚本。

It works fine when I have a file path with no special characters, but I have run into a problem with special characters. 当我的文件路径中没有特殊字符,但是我遇到特殊字符问题时,它可以正常工作。

Here is the code: 这是代码:

@ECHO OFF
setlocal EnableDelayedExpansion
set "_SCRIPT_DRIVE=%~d0"
set "_SCRIPT_PATH=%~p0"
set "PATH=!_SCRIPT_DRIVE!!_SCRIPT_PATH!"

This seems to work as desired, as when I do 就像我一样,这似乎可以按需工作

ECHO !PATH!

I get my desired output of 我得到了我想要的输出

C:\\A&B\File

However, when I later go on to run this through Rscript.exe command, it doesn't recognize the delayed expansion and executes the ampersand as a special character. 但是,当我以后继续通过Rscript.exe命令运行此命令时,它无法识别延迟的扩展,并以与号作为特殊字符执行。 The input of: 输入:

"!PATH!\R-3.0.1\bin\Rscript.exe" "!PATH!Scripts\Test.R"

Returns: 返回:

'C:\\A' is not recognized as an internal or external command, operable program or batch file.

I only have this problem, though, when I try and run Rscript.exe. 但是,当我尝试运行Rscript.exe时,只有这个问题。 I am perfectly capable of opening a file such as 我完全有能力打开一个文件,例如

"!PATH!Scripts\Test.R"

And it executes without any problems. 它执行没有任何问题。 Has anyone else run into this problem before? 有人遇到过这个问题吗? Does anyone see any problems with my syntax? 有人看到我的语法有问题吗?

EDIT: SOLVED 编辑:已解决

For anyone who stumbles upon this post in the future, I managed to find a solution to this problem. 对于以后偶然发现此帖子的任何人,我设法找到了解决此问题的方法。 It turns out that it wasn't a batch script error at all but rather a problem with Rscript.exe, I'm not sure what was going on. 事实证明,这根本不是批处理脚本错误,而是Rscript.exe的问题,我不知道这是怎么回事。 Instead of using the Rscript.exe located directly in \\bin , you must go one step further and use "!PATH!\\R-3.0.1\\bin\\i386\\Rscript.exe" which works, most likely because I am running on a 32 bit machine rather than a 64. 而不是直接使用\\bin的Rscript.exe,您必须更进一步,并使用"!PATH!\\R-3.0.1\\bin\\i386\\Rscript.exe" ,该方法很有用,这很可能是因为我在32位计算机而不是64位计算机。

With batch files, you can get this to work usually by escaping the & with a ^ . 使用批处理文件,通常可以通过使用^来转义&来使其正常工作。 I do not have a windows machine, but can you first check manually if this works, and if so, I can work with you if necessary to implement a function that does string escaping. 我没有Windows机器,但是可以先手动检查是否可以运行,如果可以,我可以在必要时与您一起实施实现字符串转义的功能。

For the substitution one of the two below shoule work ( forgive me as I have not Windows machine ), 对于下面两种工作之一的替换(请原谅我,因为我没有Windows机器),

set workable_filename=%file_name:^&=^^&%
set workable_filename=%file_name:&=^&%
@REM:: I cannot test, but one of these should work

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

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