向批处理文件传递包含引号和空格的参数
在很多情况下,我都处理过传递带有空格、引号、百分比、斜杠以及它们的各种组合的批处理文件参数。通常我都会设法弄清楚如何完成我想要的事情,但这一次我陷入了困境。我现在已经尝试了几百种组合,但我的头开始疼了。
我已将问题简化为一个相当简单的要求:从一个批处理文件传递到另一个批处理文件,参数包含一些空格分隔的文本,其中一个是带引号的空格。也就是说,一个批处理文件应将一些字符串 X
传递给另一个批处理文件,以便第二个批处理文件回显 "A "B C" D"
。我只是不知道 X
应该是什么。
这是一个最小的批处理文件,演示了一些不起作用的尝试。 (这个 BAT 文件通过调用自身来取代这两个文件。)
::Goal is to print:
::"A "B C" D"
::ie., pass from one BAT file to another a quote containing spaces and a quote containing a space
@echo off
if not (%1)==() goto print
:passarg
call %0 "A "B C" D"
call %0 "A \"B C\" D"
%0 "A ""B C"" D"
:print
echo %1
pause
这些尝试都不起作用。我尝试过使用 "\" \""
, """ """
, """" """"
, <代码>"\"" "\"", ""\" \"""
, "^" ^""
, ^" " "^"
,等等。他们要么打印双双引号,要么丢失空格后的所有内容,或者其他东西(这是错误的)。
有什么想法吗?谢谢。
On many occasions I have dealt with passing batch files arguments with spaces, quotes, percents, and slashes and all sorts of combinations of them. Usually I managed to figure out how to accomplish what I want, but this time I am stuck. I have tried a couple of hundred combinations now and my head is starting to hurt.
I’ve reduced the problem to a—fairly—simple requirement: pass from one batch file to another, an argument that contains some space-delimited text, one of which is a quoted space. That is, one batch file should pass some string X
to another so that the the second one echos "A "B C" D"
. I just can’t figure out what X
should be.
Here is a minimal batch file that demonstrates some attempts that do not work. (This BAT file takes the place of both by calling itself.)
::Goal is to print:
::"A "B C" D"
::ie., pass from one BAT file to another a quote containing spaces and a quote containing a space
@echo off
if not (%1)==() goto print
:passarg
call %0 "A "B C" D"
call %0 "A \"B C\" D"
%0 "A ""B C"" D"
:print
echo %1
pause
None of those attempts work. I’ve tried using "\" \""
, """ """
, """" """"
, "\"" "\""
, ""\" \"""
, "^" ^""
, ^"" "^"
, and so on. Either they print double double-quotes, lose everything after the space, or something else (that is wrong).
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个解决方法怎么样:
caller.bat:
callee.bat:
How about this workaround:
caller.bat:
callee.bat:
这有效
This works
我是论坛的新手。很酷,但直到现在才注册。
我喜欢从另一个批次调用一个批次的想法,非常方便,因此您可以将尽可能多的内容“打包”到一个文件中。
我知道这个线程已经很旧了,希望我不会被禁止或输出一些哈哈的东西
希望
它有帮助!
抱歉我的脚本很乱,我对编程不太了解;)
I'm new to the forum. Is quite cool, but never signed up until now.
I like the idea of calling a batch from another, very handy so you "pack" as much as possible into one single file.
I know this thread is old and hope I don't get banned or something lol
That outputs
Hope it helps!
Sorry about my messy script, I'm not too deep into programming ;)