3

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
Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56
  • 2
    Your echo of `)` is being treated as the end of the `if` block. Escape all `)` with a caret within parenthetical code blocks. Also, [always use `setlocal`](https://stackoverflow.com/a/15659309/1683264) unless you have an explicit reason not to; and if you use `goto :EOF` or `exit /b` you can get rid of the `:end` label at the end of your script. – rojo Apr 09 '17 at 20:08
  • @rocknow It is a very bad idea to name the batch file `start.bat` as this overrides the built-in command __START__ of Windows command interpreter. So it is strongly recommended to give the batch file a different name than `start.bat`. – Mofi Apr 10 '17 at 05:12

1 Answers1

4

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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Good idea with the exception of using `START` as label because __START__ is also a built-in command of Windows command interpreter. The label `START` works. But in case of using also command __START__ in the batch file, it is no good idea to use this string as label because of searching for *start* with meaning label or with meaning command becomes difficult. Better would be `BEGIN` as label. – Mofi Apr 10 '17 at 05:10
  • Awesome, it works! @Compo could you explain how the first line works (i mean conditions, not the goto)? – Alex Zaitsev Apr 10 '17 at 07:03
  • If the the third argument isn't empty, (exists), and the fourth parameter is empty, (doesn't exist), start the main part of the script. Any other scenario gets the instructions before exiting. _(BTW, you may wish to add a `TIMEOUT -1 1>NUL` just before the `GOTO :EOF` so the end user has time to read the message)_. – Compo Apr 10 '17 at 07:32