有没有办法将参数“按名称”(而不是按顺序)传递给批处理.bat文件?

时间:2011-03-07 04:09:26

标签: batch-file

我需要能够通过NAME(而不是按顺序)将参数传递给Windows批处理文件。我的目的是为最终用户提供以任何顺序传递参数的灵活性,批处理文件仍然可以处理它们。

让我的问题更清晰的一个例子:

在命令行中,用户执行以下操作: somebatchfile.bat originalFile.txt newFile.txt

somebatchfile.bat里面有一个简单的语句可以将原始文件(第一个参数%1%)的内容复制到新文件(第二个参数%2%)。它可以像下面的声明一样简单: copy %1% %2%

现在,如果用户以相反的顺序传递上述参数,结果将远非理想(实际上非​​常错误)。

那么,用户是否有办法按名称传递参数:例如somebatchfile.bat "SOURC=originalFile.txt" "TARGET=newFile.txt"以及脚本识别它们并在正确的位置使用' copy %SOURCE% %TARGET%

谢谢,

9 个答案:

答案 0 :(得分:22)

是的,你可以做类似的事情,虽然我不认为你可以使用“=”作为标记分隔符。您可以使用冒号“:”,somebatchfile.bat "SOURC:originalFile.txt" "TARGET:newFile.txt"。以下是如何拆分令牌的示例:

@echo off

set foo=%1
echo input: %foo%

for /f "tokens=1,2 delims=:" %%a in ("%foo%") do set name=%%a & set val=%%b

echo name:  %name%
echo value: %val%

运行它会产生这个:

C:\>test.bat SOURC:originalFile.txt
input: SOURC:originalFile.txt
name:  SOURC
value: originalFile.txt

[编辑]

好吧,也许昨晚我睡觉的时间太近了,但今天早上又看了,你可以这样做:

@echo off

set %1
set %2

echo source: %SOURCE%
echo target: %TARGET%

哪会产生这个(请注意,我在命令行上反转了源和目标以显示它们已正确设置和检索):

C:\>test.bat "TARGET=newFile.txt" "SOURCE=originalFile.txt"
source: originalFile.txt
target: newFile.txt

请注意,在set之前评估%1和%2,以便将它们设置为环境变量。但必须才能在命令行中引用。

答案 1 :(得分:8)

派对有点迟了:)这是我对管理“posix like”选项的建议。 例如mybatchscript.bat -foo=foovalue -bar=barvalue -flag

:parseArgs
:: asks for the -foo argument and store the value in the variable FOO
call:getArgWithValue "-foo" "FOO" "%~1" "%~2" && shift && shift && goto :parseArgs

:: asks for the -bar argument and store the value in the variable BAR
call:getArgWithValue "-bar" "BAR" "%~1" "%~2" && shift && shift && goto :parseArgs

:: asks for the -flag argument. If exist set the variable FLAG to "TRUE"
call:getArgFlag "-flag" "FLAG" "%~1" && shift && goto :parseArgs


:: your code here ...
echo FOO: %FOO%
echo BAR: %BAR%
echo FLAG: %FLAG%

goto:eof

..这里是完成工作的功能。您应该将它们放在同一个批处理文件中

:: =====================================================================
:: This function sets a variable from a cli arg with value
:: 1 cli argument name
:: 2 variable name
:: 3 current Argument Name
:: 4 current Argument Value
:getArgWithValue
if "%~3"=="%~1" (
  if "%~4"=="" (
    REM unset the variable if value is not provided
    set "%~2="
    exit /B 1
  )
  set "%~2=%~4"
  exit /B 0
)
exit /B 1
goto:eof



:: =====================================================================
:: This function sets a variable to value "TRUE" from a cli "flag" argument
:: 1 cli argument name
:: 2 variable name
:: 3 current Argument Name
:getArgFlag
if "%~3"=="%~1" (
  set "%~2=TRUE"
  exit /B 0
)
exit /B 1
goto:eof

将文件另存为mybatchscript.bat并运行

mybatchscript.bat -bar=barvalue -foo=foovalue -flag

你会得到:

FOO: foovalue
BAR: barvalue
FLAG: TRUE

答案 2 :(得分:6)

我非常喜欢的其他方式:

set c=defaultC
set s=defaultS
set u=defaultU

:initial
if "%1"=="" goto done
echo              %1
set aux=%1
if "%aux:~0,1%"=="-" (
   set nome=%aux:~1,250%
) else (
   set "%nome%=%1"
   set nome=
)
shift
goto initial
:done

echo %c%
echo %s%
echo %u%

运行以下命令:

arguments.bat -c users -u products

将生成以下输出:

users
defaultS
products

答案 3 :(得分:5)

我想看到读取提供给批处理程序的命名参数的可能性。例如:

myBatch.bat arg1  arg2 

读取参数可以按如下方式完成:

%~1 would be arg1
%~2 would be arg2

提供给批处理程序的上述参数很容易阅读,但是每次执行它时,都必须保持顺序并记住arg1应该具有的内容。为了克服它,参数可以作为键:值格式提供。命令如下所示:

mybatch.bar key1:value1 key2:value2

虽然没有直接的方法来解析这样的参数列表。我在批处理脚本中编写了以下嵌套for循环,它基本上将解析并创建环境变量,其中key1作为名称,并为其赋值value1。通过这种方式,您可以使用直接读取环境变量的方式读取所有提供的参数。

@echo off
FOR %%A IN (%*) DO (
   FOR /f "tokens=1,2 delims=:" %%G IN ("%%A") DO setLocal %%G=%%H
)

之后,您可以使用%key1%格式来读取所提供的所有参数。

HTH

答案 4 :(得分:3)

一个非常老的问题,但是我认为我找到了一种将key=value参数传递到批处理文件的巧妙方法(没有外部依赖项)。您可以使用以下MWE测试该方法:

@echo off
chcp 65001

rem name this file test.bat and call it with something like:
rem test keyc=foo keyd=bar whatever keya=123 keyf=zzz

setlocal enabledelayedexpansion

set "map=%*"
call :gettoken keya var1
call :gettoken keyb var2
call :gettoken keyc var3

rem replace the following block with your real batch
rem ************************************************
echo:
echo Map is "%map%"
echo Variable var1 is "%var1%"
echo Variable var2 is "%var2%"
echo Variable var3 is "%var3%"
echo:
echo Done.
echo:
pause
rem ************************************************

goto :eof
:gettoken
call set "tmpvar=%%map:*%1=%%"
if "%tmpvar%"=="%map%" (set "%~2=") else (
for /f "tokens=1 delims= " %%a in ("%tmpvar%") do set tmpvar=%%a
set "%~2=!tmpvar:~1!"
)
exit /b

方法相当:

  • 灵活,因为key=value参数可以与其他参数(例如文件名)混合使用,并且因为用于存储值的变量的名称与键的名称无关
  • 可扩展,因为添加键很容易
  • 很健壮,因为定义的键不允许的任何传递都将被忽略,未分配的键将得到一个空值(如果您愿意,可以轻松更改此行为,使它们得到类似null或{{1} },而不是空的

享受。

答案 5 :(得分:2)

我认为您在Windows批处理脚本中寻找getopt类型的支持,遗憾的是它并不完整。你可能得到的最接近的是使用GetOpt.btm script。通过这个,您可以使用命令执行脚本:

somebatchfile.bat /SOURC:originalFile.txt /TARGET:newFile.txt

代码:

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: GetOpt - Process command line options
:: 
:: Michael Fross
:: [email]michael@fross.org[/email]
:: [url]http://fross.org[/url]
::
:: This program scans the command line sent to it and sets various 
:: environment variables that coorespond to the settings.
::
:: It sets an OPTION_arg variable for each arg on the command line.
:: If a switch, the env var is set to 1.  If a value is given via the colon sign,
:: it's set to that value.  Note, there can not be any white space around the ':'
::
:: Use "If defined OPTION_arg" or "If %OPTION_arg eq value" to test for options
::
:: It also sets a parameter variable for each paramater entered: PARAM_1 to PARAM_n
:: PARAM_0 is a special value that contains the number of PARAMs.  Useful for looping
:: through all of them.  For example, do i = 1 to %PARAM_0 by 1 ...
::
:: In your batch file call getopt as:
::      call GetOpt.btm %$
::
:: I also recommend setting setlocal and endlocal in the host batch file so that
:: the option / param variable do not stick around after the host batch files exits.
::
:: Example usage:  BatchFile.btm /a /b:22 /longopt Parm1 Parm2 /quotedArg:"long quoted arg"
::   OPTION_a will equal 1.
::   OPTION_b will equal 22
::   OPTION_quotedArg will equal "long quoted arg"
::   OPTION_longopt will eqal 1.
::   PARAM_1 will equal Parm1
::   PARAM_2 will equal Parm2
::   PARAM_0 will be set to the number of parms, so 2 in this case
::
:: To get debug messages, set DEBUG=1.  This will give detailed information for each
:: parameter on the command line as getopt loops through the list.
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Clean up the environment before we get going
unset getopt* OPTION_* PARAM_*
set getopt_ParmCounter=1

:: If in debug mode, kick off the display by showing the number of arguments
if defined DEBUG echo GetOpt is processing %# arguments:

:: Loop through all command line arguments one at a time.
for /L %i in (1,1,%#) do (
   if defined DEBUG (echo. %+ echo Scan #%i:)

   :: If first character starts with a - or / it must be an option
   iff %@instr[0,1,%[%i]] == - .or. %@instr[0,1,%[%i]] == / then
      set getopt_Parm=%[%i]
      if defined DEBUG echo  - Item "%getopt_Parm" is an option.

      :: Set the Equal Index to the position of the colon.  0 means none was found
      set getopt_EqIdx=%@index[%getopt_Parm,:]

      :: Display the index position of the colon
      if defined DEBUG .AND. %getopt_EqIdx GE 0 echo  - Found colon at index position "%getopt_EqIdx"

      :: If the index is GE 0 then we must have a colon in the option.
      :: set the OPTION value to the stuff to the right of the colon
      iff %getopt_EqIdx ge 0 then
         set getopt_ParmName=%@instr[2, %@Dec[%getopt_EqIdx] , %getopt_Parm]
         if defined DEBUG echo  - ParmName  = "%getopt_ParmName"
         set getopt_ParmValue=%@right[%@eval[-%getopt_EqIdx-1],%getopt_Parm]
         if defined DEBUG echo  - Parmvalue = "%getopt_ParmValue"
         set OPTION_%getopt_ParmName=%getopt_ParmValue
      else
         :: This is a flag, so simply set the value to 1
         if defined DEBUG echo  - No colon found in "%getopt_Parm"
         set getopt_ParmName=%@right[%@Dec[%@len[%getopt_Parm]],%getopt_Parm]
         set getopt_ParmValue=1
         if defined DEBUG echo  - ParmName = "%getopt_ParmName"
         set OPTION_%getopt_ParmName=%getopt_ParmValue
      endiff

      :: Regardless if there was a value or not, display what is going to occur
      if defined DEBUG echo  - Setting Variable OPTION_%getopt_ParmName=%getopt_ParmValue
   else
      :: There was no / or - found, therefore this must be a paramater, not an option
      if defined DEBUG echo  - "%[%i]" is a parameter, not an option
      set PARAM_%getopt_ParmCounter=%[%i]
      set PARAM_0=%getopt_ParmCounter
      if defined DEBUG echo  - Updating Number of Parms.  PARAM_0=%PARAM_0
      if defined DEBUG echo  - Setting Variable PARAM_%getopt_ParmCounter = %[%i]
      set getopt_ParmCounter=%@Inc[%getopt_ParmCounter]
   endiff
)

:: Display additional information
iff defined DEBUG then
   echo.
   echo There were %PARAM_0 parameters found.  Setting PARAM_0=%PARAM_0
   echo.
   echo GetOpt has completed processing %# arguments.  Ending Execution.
endiff

:: Perform cleanup
unset getopt_*

答案 6 :(得分:2)

进行此尝试(纯批处理):

set PARAM_0=0
:parameters_parse

set parameter=%~1
if "%parameter%"=="" goto parameters_parse_done
if "%parameter:~0,1%"=="-" (
    set ARG_%parameter:~1%="%~2"
    shift
    shift
    goto parameters_parse
)
if "%parameter:~0,1%"=="/" (
    set ARG_%parameter:~1%="%~2"
    shift
    shift
    goto parameters_parse
)
set /a PARAM_0=%PARAM_0%+1
set PARAM_%PARAM_0%="%~1"
shift
goto parameters_parse

:parameters_parse_done

rem Insert your script here

和一些测试:

call args.bat /bar="Hello World" Test1 -baz "Test test test" /foo=Test1 Test2

echo foo=%ARG_foo%
echo bar=%ARG_bar%
echo baz=%ARG_baz%
echo count=%PARAM_0%
echo 1=%PARAM_1%
echo 2=%PARAM_2%

输出:

foo="Test1"
bar="Hello World"
baz="Test test test"
count=2
1="Test1"
2="Test2"

答案 7 :(得分:1)

我的天哪;你想多了! :)

OP 的 CMD 参数都采用 "[VarName]=[Something]" 形式

所以第一行可以简单地为 FOR %%_ IN (%*) DO SET "%%~_"

或者更好看的代码

FOR %%_ IN (%*) DO SET "%%~_"

也就是说,这里有一个更完整的脚本示例,您可以将所有脚本放在 :Main 函数中

@( Setlocal
  ECHO OFF
  FOR %%_ IN (%*) DO SET "%%~_"
)

CALL :Main

( ENDLOCAL
  EXIT /B )

:Main
  REM Your code goes here.
  REM Your code goes here.
  REM Your code goes here.
  REM Your code goes here.
GOTO :EOF

答案 8 :(得分:0)

:parse
IF "%~1"=="" GOTO endparse
ECHO "%~1"| FIND /I "=" && SET "%~1"
SHIFT /1
GOTO parse
:endparse

此代码检查所有参数,删除所有外引号,如果存在等号,则将变量设置为值。

基于这个答案:Using parameters in batch files at DOS command line