Michał M
Michał M

Reputation: 618

How to reference the first parameter of a Windows batch file in an IF condition?

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?

Upvotes: 1

Views: 198

Answers (1)

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 1

Related Questions