我認爲你正在尋找Windows批處理腳本中的getopt類型的支持,但不幸的是它不存在整體。您可能最接近的是使用GetOpt.btm script。有了這個,你可以執行你的腳本中使用命令:
somebatchfile.bat /SOURC:originalFile.txt /TARGET:newFile.txt
代碼:
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: GetOpt - Process command line options
::
:: Michael Fross
:: [email][email protected][/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_*
這個問題是這樣的:[指定命令行參數像shell腳本的名稱=值對](http://stackoverflow.com/a/43008569/3957754)希望這有助於。 – JRichardsz 2017-03-24 20:33:37
但OP的問題是關於_batch_(windows/dos),與_bash_完全不同。 – 2017-11-07 23:31:54