我想将一个空格分隔的变量传递到一个批处理文件中,例如:

c:\applications\mi_pocess.bat A1 1AA

当我在 echo %1 中运行 mi_process 时,它​​返回 A1
我将如何将 A1 1AA 识别为单个字符串?

我试过在我的外部软件中用双引号包裹它
c:\applications\mi_pocess.bat + chr$(34) + A1 1AA + Chr$(34)

并且 echo %1 现在返回 "A1 1AA" (我不想要变量中的引号)

谢谢

最佳答案

我确定您知道 .bat 中的 %1%2 等表示在命令行上传递给 .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.

运行:
space-in-args.bat "a b c d e"

输出是:
 "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.

另请参阅 for /?(向下滚动到最后)以了解更多这些转换。

关于batch-file - 如何将空格分隔的变量传递到 bat 文件中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3466249/

10-10 12:45