windows 如何批量传递多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4871620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:02:09  来源:igfitidea点击:

How to pass multiple params in batch?

windowsbatch-filecommand-line

提问by IAdapter

In my batch file I want to pass multiple parameters to some other application.

在我的批处理文件中,我想将多个参数传递给其他一些应用程序。

Now I do it

现在我做到了

app.exe %1 %2

and it can only pass two parameters, but I want to pass all the parameters that are passed to the batch(I would rather not write %1 %2 %3 %4 ...)

它只能传递两个参数,但我想传递所有传递给批处理的参数(我宁愿不写%1 %2 %3 %4 ...

Is there any magic way to do it?

有什么神奇的方法可以做到吗?

回答by IAdapter

There is a magic way! I knew it, but I could not remember it.

有一个神奇的方法!我知道,但我想不起来了。

its %*

它的 %*

回答by Andriy M

Another way is to use one double quoted parameter. When calling the other application, you just use the %~Ndevice on the command line to remove the quotes.

另一种方法是使用一个双引号参数。调用其他应用程序时,您只需使用%~N命令行上的设备来删除引号。

If some parameters you intend to pass to the application are themselves double-quoted, those quote chars must be repeated twice.

如果您打算传递给应用程序的某些参数本身是双引号的,则这些引号字符必须重复两次。

Here's an illustration script that uses the first parameter as the application's name and the second as a combined parameter list to pass to the application:

这是一个插图脚本,它使用第一个参数作为应用程序的名称,将第二个参数用作传递给应用程序的组合参数列表:

@ECHO OFF
CALL %1 %~2

Here are examples of calling the script for different cases (pass one parameter or several parameters or quoted parameters).

以下是针对不同情况调用脚本的示例(传递一个参数或多个参数或引用参数)。

  1. Pass 1 parameter to the app:

    C:\>mybatch.bat app.exe "app_param"
    C:\>mybatch.bat app.exe app_param
    
  2. Pass several parameters:

    C:\>mybatch.bat app.exe "app_param1 app_param2 app_param3"
    
  3. Pass a parameter that includes spaces (and so must be quoted):

    C:\>mybatch.bat app.exe """parameter with spaces"""
    
  4. A combined example: some parameters are with spaces, others aren't:

    C:\>mybatch.bat app.exe "param_with_no_spaces ""parameter with spaces"" another_spaceless_param"
    
  1. 将 1 个参数传递给应用程序:

    C:\>mybatch.bat app.exe "app_param"
    C:\>mybatch.bat app.exe app_param
    
  2. 传递几个参数:

    C:\>mybatch.bat app.exe "app_param1 app_param2 app_param3"
    
  3. 传递一个包含空格的参数(因此必须被引用):

    C:\>mybatch.bat app.exe """parameter with spaces"""
    
  4. 一个组合示例:一些参数带有空格,而另一些则没有:

    C:\>mybatch.bat app.exe "param_with_no_spaces ""parameter with spaces"" another_spaceless_param"
    

回答by Simon Campbell

You could use the SHIFTprompt and loop through the arguments. Here is a demonstrative example where you would replace the final ECHO prompt with a prompt to load your application.

您可以使用SHIFT提示并遍历参数。下面是一个演示示例,您可以用加载应用程序的提示替换最终的 ECHO 提示。

@ECHO OFF

SET PARAMS=

:_PARAMS_LOOP

REM There is a trailing space in the next line; it is there for formatting.
SET PARAMS=%PARAMS%%1 
ECHO %1
SHIFT

IF NOT "%1"=="" GOTO _PARAMS_LOOP

ECHO %PARAMS%

PAUSE

This may be useful if you need some sort of dynamic parameter counting, or if you want to disallow a certain parameter.

如果您需要某种动态参数计数,或者您想禁止某个参数,这可能很有用。