检查 DOS 批处理文件中的参数

检查 DOS 批处理文件中的参数

我们有一个接受参数的批处理文件。

然后我们使用 %1 作为第一个参数读取该参数的值。

问题是:我们如何检查 %1 是否有值?

答案1

您可以根据 %1 的值进行分支。例如,一种方法是

if "%1"=="" goto bad

:good

rem Do processing here

goto end

:bad

rem Do error handling here

:end

答案2

根据http://www.robvanderwoude.com/parameters.php你可以用 if 检查它们:

  • 如果“%1”==“”表示非引号参数

或者

  • 如果 [%1]==[]

或者

  • IF "%~1"==""(仅限 NT4+SP6 及更高版本)

答案3

以下是我针对此问题的解决方案。我使用这种样式来使用“命名参数”而不是传统的位置值。这样,就可以对默认和使用的参数进行验证。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: batchparms.bat = This is an example of named parameters in a batch file. ::
:: This demonstrates the ability to name parameters instead of the current  ::
:: positional restrictions provided natively.                   ::
::                                      ::
:: Parameters:                                  ::
:: -first   = First named parm. Should be AAA BBB or CCC [Default is AAA]   ::
:: -second  = Second named parm. Should be XXX YYY or ZZZ [Default is ZZZ]  ::
:: -flagone = First named switch [Default is TRUE]              ::
:: -flagtwo = Second named switch [Default is TRUE]             ::
::                                      ::
:: 02/19/2010   - HighDesertRaider                      ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                                      ::
:: 02/22/2012   - Modified to be used as a Demo                 ::
::      - HighDesertRaider                      ::
::                                      ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Set Environment
::
:: These flags need to be set off to use named parameters
::
SET FLAG_FIRST=0
SET FLAG_SECOND=0
SET FLAG_ONE=0
SET FLAG_TWO=0
::
:: Set Default Values
::
CLS
SET PARM_FIRST=AAA
SET PARM_SECOND=ZZZ
SET PARM_ONE=1
SET PARM_TWO=1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Parse the input parameters, and assign the arguments to variables.       ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR %%A in (%*) DO (
IF !FLAG_FIRST!==1 (
  SET PARM_FIRST=%%A
  SET FLAG_FIRST=0
) ELSE (
IF !FLAG_SECOND!==1 (
  SET PARM_SECOND=%%A
  SET FLAG_SECOND=0
) ELSE (
IF /I %%A==-FIRST (
  SET FLAG_FIRST=1
) ELSE (
IF /I %%A==-SECOND (
  SET FLAG_SECOND=1
) ELSE (
IF /I %%A==-FLAGONE (
  SET PARM_ONE=0
) ELSE (
IF /I %%A==-FLAGTWO (
  SET PARM_TWO=0
) ELSE (
  SET BADPARMMSG=Parameter %%A is not a valid option.
  GOTO:BadFormExit)))))))
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmFirst                              ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmFirst
ECHO  Validate -ParmFirst %PARM_FIRST%
FOR %%A in (AAA BBB CCC) DO (
    IF /I %PARM_FIRST%==%%A (
        GOTO ValidParmFirst))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmFirst
ECHO Please select one of the following values:
ECHO AAA
ECHO BBB
ECHO CCC
GOTO BadFormExit
:ValidParmFirst
ECHO  Value %PARM_FIRST% is valid for -ParmFirst
ECHO.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Validate -ParmSecond                             ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ValidateParmSecond
ECHO  Validate -ParmSecond %PARM_SECOND%
FOR %%A in (XXX YYY ZZZ) DO (
    IF /I %PARM_SECOND%==%%A (
        GOTO ValidParmSecond))
::
:: A Valid Parm Value was not found in the list
::
SET BADPARMMSG=Value %PARM_FIRST% is not a valid value for -ParmSecond
ECHO Please select one of the following values:
ECHO XXX
ECHO YYY
ECHO ZZZ
GOTO BadFormExit
:ValidParmSecond
ECHO  Value %PARM_SECOND% is valid for -ParmSecond
ECHO.
::
::
::
ECHO.
ECHO The value of -first is %PARM_FIRST%
ECHO The value of -second is %PARM_SECOND%
ECHO The value of -flagone is %PARM_ONE%
ECHO The value of -flagtwo is %PARM_TWO%
ECHO.
GOTO:RunMyCodeCauseIGotGoodParms
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This function shows the user the correct form to use             ::
::  and exits the script.                           ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:BadFormExit
ECHO.
ECHO  Sorry friend, but your form is bad.
ECHO.
ECHO  batchparms.bat %* is not correct.
ECHO  Reason: %BADPARMMSG%
ECHO. 
ECHO  the correct syntax for this command is:
ECHO "batchparms.bat [-first AAA | BBB | CCC] [-second XXX | YYY | ZZZ] [-flagone] [-flagtwo]"
EXIT /B 1
:RunMyCodeCauseIGotGoodParms
::
:: Insert Code Here to Run once Parms are Validated
::
EXIT /B

参数值可以列在文件中,这样您不必更改批处理文件,只需添加新值。并且可以添加更多文本来解释值等。

答案4

你可以尝试这个

设置测试=%1

如果 %test%.==. (回显测试未设置)

相关内容