如何在批处理文件(或从Windows命令行)中转义&符号,以便使用该start命令在URL中打开带有&符号的网页?
双引号不适用start; 这会启动一个新的命令行窗口.
更新1:Wael Dalloul的解决方案有效.此外,如果URL中存在URL编码字符(例如,空格编码为%20)并且它位于批处理文件中,则'%'必须编码为'%%'.在示例中不是这种情况.
例如,从命令行(CMD.EXE):
start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8
Run Code Online (Sandbox Code Playgroud)
会导致
http://www.google.com/search?client=opera
Run Code Online (Sandbox Code Playgroud)
在默认浏览器中打开并在命令行窗口中显示以下错误:
'rls' is not recognized as an internal or external command,
operable program or batch file.
'q' is not recognized as an internal or external command,
operable program or batch file.
'sourceid' is not recognized as an internal or external command,
operable program or batch file.
'ie' is not recognized as an internal or external command,
operable program or batch file. …Run Code Online (Sandbox Code Playgroud) 我想使用修改Windows PATH变量setx.以下在Windows 8上至少有50%的时间可以正常工作:
setx PATH %PATH%;C:\Python27\;C:\Python27\Scripts\
Run Code Online (Sandbox Code Playgroud)
如果它给出错误"默认参数只能使用2次",那么下面的部分时间会起作用:
setx PATH "%PATH%;C:\Python27\;C:\Python27\Scripts\"
Run Code Online (Sandbox Code Playgroud)
不同之处在于我们将第二个参数包装在引号中.我相信%PATH%扩展包含空格时引号是必要的.
但是,我在Windows 7上遇到了一些奇怪的问题.在一台特定的Windows 7机器上,我遇到了这个问题:
echo %PATH%
Run Code Online (Sandbox Code Playgroud)
它打印:
C:\Foo\;C:\Bar\;[...lots of stuff...]C:\Baz\
Run Code Online (Sandbox Code Playgroud)
然后我这样做:
setx PATH "%PATH%;C:\Quux\"
Run Code Online (Sandbox Code Playgroud)
然后它说"错误:截断1,024个字符." 现在让我们检查一下PATH包含的内容:
echo %PATH%
Run Code Online (Sandbox Code Playgroud)
它打印:
C:\Foo\;C:\Foo\;C:\Bar\;C:\Bar\;[...lots of stuff, now duplicated...]C:\B
Run Code Online (Sandbox Code Playgroud)
......它被裁减为1,024个字符.由于重复,它跑了过来.同样有趣的是:尽管setx引发了错误而没有说"成功" ,但PATH的价值也发生了变化.
我能够多次重复这种奇怪的行为(幸运的是我保存了PATH的原始内容).
目前,我知道附加到PATH的唯一可靠方法如下:
echo 路径.
将PATH的内容复制到文本文件中,然后手动添加;C:\Python27\;C:\Python27\Scripts\到PATH的末尾.
将整个事物复制出文本文件.
setx PATH "<paste the string here>"
该过程在Windows 7和Windows 8上每次都有效.
我应该能够在一个命令中做到这一点.我究竟做错了什么?
谢谢.
我有一个批处理文件,将文件从一个文件夹移动到另一个文件夹 批处理文件由另一个进程生成.
我需要移动的一些文件中包含字符串"%20":
move /y "\\myserver\myfolder\file%20name.txt" "\\myserver\otherfolder"
Run Code Online (Sandbox Code Playgroud)
这会失败,因为它试图找到一个名称为的文件:
\\myserver\myfolder\file0name.txt
Run Code Online (Sandbox Code Playgroud)
有什么办法可以忽略%吗?我无法改变生成的文件以逃避这种情况,例如加倍百分号(%%),逃避/或^(插入符号)等.
这是我尝试将我的应用程序可执行文件复制到另一个文件夹更改它的名称:
IF $(ConfigurationName) == Release (
SET DESTINATION=$(ProjectDir)Output\Distribution
IF NOT EXIST "%DESTINATION%" ( MD "%DESTINATION%" )
XCOPY /Q /Y "$(TargetPath)" "%DESTINATION%"
RENAME "%DESTINATION%\$(TargetFileName)" "$(TargetName).Plain$(TargetExt)"
)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切使它工作,但它总是抛出错误代码255或1,这取决于.使用普通批处理文件运行该代码就像一个魅力!
我可以通过一个双引号,并且比大牌子的任何命令在几个方面:'"',"\"",">"
但是当我尝试将它们一起传递时
C:\>echo "\">"
The system cannot find the path specified.
Run Code Online (Sandbox Code Playgroud)
与"\"\>". 我可以让它与单引号一起工作,但由于我已经有很多处理引号的工作,我想将它全部保留在双引号内。
有什么办法可以逃避吗?
我在 windows7 上,但我相信这是一些向后兼容的“功能”,所以不确定这个信息是否相关。
编辑1:
我认为 Endoro 的答案是正确的……但事情没那么简单。CMD^>根据字符串中是否有转义双引号进行不同处理。任何人都知道为什么?!或不同的转义方法?
C:\>sh echo "\"^>"
">
C:\>sh echo "a^>"
a^>
C:\>echo "\"^>"
"\">"
C:\>echo "a^>"
"a^>"
Run Code Online (Sandbox Code Playgroud)
编辑 2:这里是 Monacraft 建议的测试用例,^在字符串周围的引号之前使用
C:\>echo ^"a/>"
The system cannot find the path specified.
(we still need to escape that > symbol)
C:\>echo ^"a/^>"
"a/>"
(work fine without \" in the string)
C:\>echo ^"\"/^>"
"\"/^>" …Run Code Online (Sandbox Code Playgroud) 我是Windows批处理编程的新手.我想要实现的是为Java应用程序编写启动脚本.然而,它并没有启动Java应用程序,而是打印出来
用法:java [-options] class [args ...]
(执行课程)
或java [-options] -jar jarfile [args ...]
(执行jar文件)
...
这表示我的参数未被正确识别.
这是我的MCVE for not-working script:
set memory=600000
java -XX:OnOutOfMemoryError="taskkill /PID %p" -Xmx%memory%K -jar MyApp.jar
Run Code Online (Sandbox Code Playgroud)
在实际场景中,memory计算为应用程序设置最佳最大堆大小.
省略其中一个参数会使应用程序启动.所以
java -XX:OnOutOfMemoryError="taskkill /PID %p" -jar MyApp.jar
Run Code Online (Sandbox Code Playgroud)
和
set memory=600000
java -Xmx%memory%K -jar MyApp.jar
Run Code Online (Sandbox Code Playgroud)
工作,但我需要两个参数在一个调用中工作.
我正在尝试从 MSBuild(VS2015,从文件)执行这个简单的命令.target来生成当前 git 提交的日期:
git show -s --format=%cd --date=format:%d.%m.%Y
Run Code Online (Sandbox Code Playgroud)
所以在 MSBuild 中我尝试过:
<Exec Command="git show -s --format=%25cd --date=format:%25d.%25m.%25Y" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="BuildDate" />
</Exec>
Run Code Online (Sandbox Code Playgroud)
但它只会产生一个错误:
1>------ Build started: Project: example, Configuration: Release Dll Win32 ------
1> fatal: invalid --pretty format: d.Y
1>D:\example\gitversion.targets(26,5): error MSB3073: The command "git show -s --format=%cd --date=format:%d.%m.%Y" exited with code 128.
Run Code Online (Sandbox Code Playgroud)
如果我将引号内的命令发布到控制台,它会像魅力一样工作并打印19.12.2016.
我尝试过以下操作:
逃脱也=标志,,:...仍然不起作用
仅使用Command="git show -s --format=%25ci"-> 也会产生错误fatal: invalid --pretty format: ci,但在控制台中工作正常。
用引号引起来 …
batch-file ×4
windows ×4
cmd ×2
escaping ×2
.net ×1
arguments ×1
c# ×1
command-line ×1
java ×1
msbuild ×1
parameters ×1
path ×1
setx ×1