如何逗号分隔的参数传递到批处理文件,如果在其他批处理文件中使用? [英] how to pass comma separated parameter to batch file and use if else in batch file?

查看:274
本文介绍了如何逗号分隔的参数传递到批处理文件,如果在其他批处理文件中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件MyTest.bat

I have one batch file MyTest.bat

MyTest.bat

call "first.bat" 

call "second.bat" 

call "third.bat"

现在在执行MyTest.bat我会把逗号分隔的参数,如

Now while executing MyTest.bat I will pass comma separated parameters like

打电话MyTest.bat first.bat,second.bat

call MyTest.bat first.bat,second.bat

现在里面MyTest.bat我想检查哪些参数传递和基于使用的,如果else条件

now inside MyTest.bat I want to check which parameters are passed and based on those using if else condition

我想执行内部语句。

例如类似的东西。

MyTest.bat first.bat,second.bat

now inside 

I will check 

get all parameters list param[] = {first.bat,second.bat}

if param[i] ==  "first.bat" 

{

call "first.bat" 

}

else if param[i] ==  "second.bat" 


{

call "second.bat" 

}

else if param[i] ==  "third.bat" 


{

call "third.bat" 

}

//这保证了哪些参数只有那些语句被执行而不是其他。

// this assures that what parameters I passed only those statements gets executed not other.

以上code只是一个伪code怎么能写实际MyTest.bat?

The above code is just a pseudo code how can write actual MyTest.bat?

推荐答案

下一个脚本需要另一个参数顺序(批下来的名字,结束所有其它参数列表):

Next script requires another parameter order (list of batch names down to end all other parameters):

@ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749.bat name address "first script", second, third
:loop
  if "%~3" == "" goto :next
  if exist "%~n3.bat" (
    call "%~n3.bat" %1 %2
  ) else (
    echo can't found file; failed call "%~n3.bat" %1 %2 
  )
  shift /3
  goto :loop
:next

有关调试目的,prepare样本文件第一script.bat second.bat ;确保 third.bat 不存在:

For debugging purposes, prepare sample files "first script.bat" and second.bat; ensure that third.bat does not exist:

==> >"first script.bat" echo @echo %~nx0 parameters: %%*=%*

==> >second.bat echo @echo %~nx0 parameters: %%*=%*

==> 2>NUL del third.bat

输出(表明上使用分隔符的独立性):

Output (shows independency on used delimiters):

==> 33955749 name address "first script", second, third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address

==> 33955749 name address "first script"  second; third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address

另一种方法:拳头参数=一对双引号包围逗号分隔的一批名单:

Another approach: fist parameter = list of comma-separated batch names surrounded with a pair of double quotes:

@ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749b "first script,second,third" name address
rem no spaces surrounding commas or names
rem wrong: 33955749b " first script , second, third" would fail
set "_names=%~1"
set "_names=%_names:,=","%"
rem debug echo _names="%_names%"
for  %%G in ("%_names%") do (
  if exist "%%~dpnG.bat" (
    call "%%~dpnG.bat" %2 %3
  ) else (
    echo can't found script; failed call "%%~dpnG.bat" %2 %3 
  )
)

输出(显示响应所用的分隔符):

Output (shows responsivity to used delimiters):

==> 33955749b "first script,second,third" name address
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\third.bat" name address

==> 33955749b "first script, second,third" name address
first script.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\ second.bat" name address
can't found script; failed call "D:\bat\SO\third.bat" name address

请注意,这两个 33955749.bat 33955749b.bat 脚本


  • 接受(部分或完全限定)路径称为脚本,即使有空间(S);

  • 在另一方面
  • ,他们都忽略了文件的扩展名(S),即使提供,力的.bat

  • accept (partially or fully qualified) paths to called scripts, even with space(s);
  • on the other hand, they both ignore file extension(s) even if supplied and force .bat.

例如, 33955749名称地址first.cmd second.cmd 将试图调用 first.bat second.bat

资源(必读书,不完全):

Resources (required reading, incomplete):

  • (command reference) An A-Z Index of the Windows CMD command line
  • (additional particularities) Windows CMD Shell Command Line Syntax
  • (%~G, %~1 etc. special page) Command Line arguments (Parameters)
  • (%variable:StrToFind=NewStr% etc.) Variable Edit/Replace

这篇关于如何逗号分隔的参数传递到批处理文件,如果在其他批处理文件中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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