Alex Zaitsev
Alex Zaitsev

Reputation: 1781

check number of arguments on batch file

I'm trying to do simple checking on the batch file. argCount contains correct number but I have a trouble in comparing variable and number. I want to show help if number of arguments is not equal to 3 and go to the end of the file.
I tried:
if not %argCount% == 3
if not %argCount%=='3'
if not '%argCount%'=='3'
if %argCount% NEQ 3
but none of these options works as expected... Most of options that I tried always show me help message regardless of the number of arguments, some of the options show me help message without first 3 lines if I pass 3 arguments to the script (extremely weird).

@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

Upvotes: 3

Views: 10795

Answers (1)

Compo
Compo

Reputation: 38579

Why not just simplify the structure:

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

Upvotes: 4

Related Questions