我想将一个空格分隔的变量传递到一个批处理文件中,例如:
c:\applications\mi_pocess.bat A1 1AA
Run Code Online (Sandbox Code Playgroud)
当我跑echo %1进去的时候mi_process它回来了A1
我将如何识别A1 1AA为单个字符串?
我试过在我的外部软件中用双引号包裹它
c:\applications\mi_pocess.bat + chr$(34) + A1 1AA + Chr$(34)
Run Code Online (Sandbox Code Playgroud)
而echo %1现在返回"A1 1AA"(我不想在变量引号)
谢谢
我相信大家都知道,%1,%2,等内.BAT代表传递到在命令行中的.bat编号的参数。
如果您将它们用作%~1,%~2等。如果它们在那里,所有周围的引号将被自动删除。
考虑这个space-in-args.bat进行测试:
@echo off
echo. %1
echo. (original first argument echo'd)
echo.
echo. "%1"
echo. (original first argument with additional surrounding quotes)
echo.
echo. %~1
echo. (use %%~1 instead of %%1 to remove surrounding quotes, should there be)
echo.
echo. "%~1"
echo. (we better use "%%~1" instead of "%%1" in this case:
echo. 1. ensure, that argument is quoted when used inside the batch;
echo. 2. avoid quote doubling should the user have already passed quotes.
echo.
Run Code Online (Sandbox Code Playgroud)
运行:
space-in-args.bat "a b c d e"
Run Code Online (Sandbox Code Playgroud)
输出是:
"a b c d e"
(original first argument echo'd)
""a b c d e""
(original first argument with additional surrounding quotes)
a b c d e
(using %~1 instead of %1 removes surrounding quotes, should there be some)
"a b c d e"
(we better use "%~1" instead of "%1" in this case:
1. ensure, that argument is quoted when used inside the batch;
2. avoid quote doubling should the user have already passed quotes.
Run Code Online (Sandbox Code Playgroud)
另请参阅for /?(向下滚动到最后)以了解更多这些转换。