如何将空格分隔的变量传递到bat文件中? [英] How can I pass a space separated variable into a bat file?

查看:287
本文介绍了如何将空格分隔的变量传递到bat文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am wanting to pass a space separated variable into a batch file like:

c:\applications\mi_pocess.bat A1 1AA

当我在mi_process中运行echo %1时,它返回A1

when I run echo %1 in mi_process it returns A1

如何识别A1 1AA为单个字符串呢?

How would I go about it recognising A1 1AA to be a single string?

我曾尝试将其用双引号括在我的外部软件中,例如

I have tried wrapping it in Double quotes in my external software like

c:\applications\mi_pocess.bat + chr$(34) + A1 1AA + Chr$(34)

echo %1现在返回"A1 1AA"(我不希望变量中使用引号)

and echo %1 now returns "A1 1AA" (I do not want the quote marks in the variable)

谢谢

推荐答案

我确定您知道.bat中的%1%2等表示传递给.bat的编号参数.命令行.

I'm sure you are aware that %1, %2, etc. inside a .bat represent the numbered arguments passed to the .bat on the commandline.

如果将它们用作%~1%~2等,则所有周围的引号都会自动删除,只要它们在那里.

If you use them as %~1, %~2, etc. all surrounding quotes will be removed automatically, should they be there.

考虑此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 /?(向下滚动至末尾),以了解更多有关这些转化的信息.

See also for /? (scroll down towards the end) to find out about some more of those conversions.

这篇关于如何将空格分隔的变量传递到bat文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆