Matthew MacGregor
Matthew MacGregor

Reputation: 13

Spaces in a if statement

I can't seem to get the if statement to work for Hello World. I think it is because of the spaces but any help would be appreciated.

if %A%==Chrome (
    start "" "C:\Users\Matthew\Appdata\Local\Google\Chrome\Application\chrome.exe\"
) else (
    if %A%==Steam (
        start "" "C:\Program Files (x86)\Steam\Steam.exe\"
    ) else (
        if %A%==CMD (
            start
        ) else (
            if %A%==System32 (
                start "" "C:\Windows\System32\"
            ) else (
                if %A%==Appdata (
                    start "" "C:\Users\Matthew\Appdata\"
                ) else (
                    if %A%==Skype (
                        goto Skype
                    ) else (
                        if "%A%"=="Hello World" (
                            goto HelloWorld
                        )
                    )   
                )
            )           
        )
    )
)

Upvotes: 0

Views: 2024

Answers (1)

MC ND
MC ND

Reputation: 70923

If the contents of %A% include spaces (Hello world), the first condition will fail as it will be parsed as

if Hello World==Chrome (

If you expect to have spaces in your variable, you will need to use quotes not in the last condition but in all your conditions

if "%A%=="Chrome" (
    start "" "C:\Users\Matthew\Appdata\Local\Google\Chrome\Application\chrome.exe\"
) else if "%A%"=="Steam" (
    start "" "C:\Program Files (x86)\Steam\Steam.exe"
) else if "%A%"=="CMD" (
    start
) else if "%A%"=="System32" (
    start "" "C:\Windows\System32\"
) else if "%A%"=="Appdata" (
    start "" "C:\Users\Matthew\Appdata\"
) else if "%A%"=="Skype" (
    goto Skype
) else if "%A%"=="Hello World" (
    goto HelloWorld
)

Upvotes: 3

Related Questions