向批处理文件传递包含引号和空格的参数

发布于 2024-08-28 10:49:09 字数 915 浏览 6 评论 0原文

在很多情况下,我都处理过传递带有空格、引号、百分比、斜杠以及它们的各种组合的批处理文件参数。通常我都会设法弄清楚如何完成我想要的事情,但这一次我陷入了困境。我现在已经尝试了几百种组合,但我的头开始疼了。

我已将问题简化为一个相当简单的要求:从一个批处理文件传递到另一个批处理文件,参数包含一些空格分隔的文本,其中一个是带引号的空格。也就是说,一个批处理文件应将一些字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情废物 2024-09-04 10:49:09

这个解决方法怎么样:

caller.bat:

@echo off
echo "A "B C" D">dummy.txt
call callee.bat

callee.bat:

@echo off
set /p argument=<dummy.txt
echo %argument%
pause

How about this workaround:

caller.bat:

@echo off
echo "A "B C" D">dummy.txt
call callee.bat

callee.bat:

@echo off
set /p argument=<dummy.txt
echo %argument%
pause
恬淡成诗 2024-09-04 10:49:09

这有效

@echo off
if not (%1)==() goto print
:passarg
  call %0 "A "B C" D"
:print
  echo %*

This works

@echo off
if not (%1)==() goto print
:passarg
  call %0 "A "B C" D"
:print
  echo %*
锦上情书 2024-09-04 10:49:09

我是论坛的新手。很酷,但直到现在才注册。

我喜欢从另一个批次调用一个批次的想法,非常方便,因此您可以将尽可能多的内容“打包”到一个文件中。

我知道这个线程已经很旧了,希望我不会被禁止或输出一些哈哈的东西

:Example.bat
@echo off

set SOMEARG=%1
set EXAMPLEARG=%2
set EXAMPLEARG=%EXAMPLEARG:""="%
set NEWFOLDER=%3
if not (%1)==() GOTO PRINT

CALL %0 SomeWord "Hey this arg has quoted "" "" spaces!" "C:\Program Files\Folder with spaces\Subfolder"

:PRINT
ECHO.
ECHO %0
ECHO %SOMEARG%
ECHO %EXAMPLEARG%
ECHO %NEWFOLDER%
ECHO.
pause

希望

"C:\SUT_Tools\Scripts\Testing\GLSU\Example.bat"
SomeWord
"Hey this arg has quoted " " spaces!"
"C:\Program Files\Folder with spaces\Subfolder"

Press any key to continue . . .

它有帮助!

抱歉我的脚本很乱,我对编程不太了解;)

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

:Example.bat
@echo off

set SOMEARG=%1
set EXAMPLEARG=%2
set EXAMPLEARG=%EXAMPLEARG:""="%
set NEWFOLDER=%3
if not (%1)==() GOTO PRINT

CALL %0 SomeWord "Hey this arg has quoted "" "" spaces!" "C:\Program Files\Folder with spaces\Subfolder"

:PRINT
ECHO.
ECHO %0
ECHO %SOMEARG%
ECHO %EXAMPLEARG%
ECHO %NEWFOLDER%
ECHO.
pause

That outputs

"C:\SUT_Tools\Scripts\Testing\GLSU\Example.bat"
SomeWord
"Hey this arg has quoted " " spaces!"
"C:\Program Files\Folder with spaces\Subfolder"

Press any key to continue . . .

Hope it helps!

Sorry about my messy script, I'm not too deep into programming ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文