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

时间:2017-04-09 19:49:21

标签: batch-file

我正在尝试对批处理文件进行简单检查。 argCount包含正确的数字,但我在比较变量和数字方面遇到了麻烦。如果参数的数量不等于3并且转到文件的末尾,我想显示帮助 我试过了:
if not %argCount% == 3
if not %argCount%=='3'
if not '%argCount%'=='3'
if %argCount% NEQ 3
但是这些选项都没有按预期工作...我尝试的大多数选项总是向我显示帮助消息,无论参数的数量如何,如果我将3个参数传递给脚本,一些选项会显示帮助消息而不需要前3行(非常奇怪)。

@echo off

set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
)
if not %argCount% == 3 (
    echo This script requires the next parameters:
    echo - absolute path to file
    echo - filter (explanation)
    echo - true or false (explanation)
    echo Examples:
    echo start.bat full\path\to\the\file.ext test true
    echo start.bat full\path\to\the\file.ext nof false
    goto end
)

REM some another code

:end

1 个答案:

答案 0 :(得分:4)

为什么不简化结构:

IF NOT "%~3"=="" IF "%~4"=="" GOTO START
ECHO This script requires the next parameters:
ECHO - absolute path to file
ECHO - filter (explanation)
ECHO - true or false (explanation)
ECHO Examples:
ECHO "%~nx0" "full\path\to\the\file.ext" test true
ECHO "%~nx0" "full\path\to\the\file.ext" nof false
GOTO :EOF

:START