且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何逗号分隔的参数传递到批处理文件,如果在其他批处理文件中使用?

更新时间:2023-10-27 17:06:58

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

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