1

I'm very new in cmd batch files. I have code:

@echo off
if {%1} =="1" (
    goto 1cmd
)
if {%1} =="2" (
    goto 2cmd
)
if {%1} =="3" (
    goto 3cmd
)
if {%1} =="" (
    echo qwerty
)

:1cmd
call D:\test\1\1.cmd
goto end

:2cmd
call D:\test\2\2.cmd
goto end

:3cmd
call D:\test\3\3.cmd
goto end

:end

File is named a.bat. No matter what parameter I type, a.bat always calls 1.cmd.

What is the reason?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Michał M
  • 618
  • 5
  • 13
  • You need to enter `{2}` to choose option 2. You choose this. –  May 05 '16 at 12:08
  • You need to change `{%1}` to `"%~1"`. Then you need to consider the case when somebody enters `4`... – aschipfl May 05 '16 at 12:43
  • Possible duplicate of [How to pass command line parameters to a batch file?](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-to-a-batch-file) – Mofi May 05 '16 at 20:51

1 Answers1

1

Is this working ?

@ECHO OFF


        if "%~1" =="1" (
        goto 1cmd
        )
         if "%~1" =="2" (
        goto 2cmd
        )
         if "%~1" =="3" (
        goto 3cmd
        )
    if {%1} =="" (
    echo qwerty
    )
    exit /b 0


        :1cmd
        call D:\test\1\1.cmd    
        goto end

        :2cmd
        call D:\test\2\2.cmd    
        goto end
        :3cmd
        call D:\test\3\3.cmd
goto end
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Yes it is. Could you explain me why "%1" or {%1} didn't work? I based on other script from outer application, and in this script everything worked. Mayby some missing directives? – Michał M May 05 '16 at 12:30
  • @MichałM `if something == somethin` compares the strings directly.In case some of the arguments contains spaces you need quotes ,BUT quotes also will be evaluated. the `%~1` is dequoted first argument and here is used for robustness. – npocmaka May 05 '16 at 12:33
  • Understood. And one more - exit /b 0 <- ? – Michał M May 05 '16 at 12:34
  • @MichałM - if there is no `exit /b` or `goto :eof` the script will execute the code in the labels. Though here it is not necessary as you are using `GOTO` , but if it is `CALL :label` the exit will be crucial.Usually `exit` works a little bit faster than `goto :eof` – npocmaka May 05 '16 at 12:37