LankymanX
LankymanX

Reputation: 51

How to IF check and do multiple things else in CMD

===== CMD BATCH FILE=====

I am trying to check a variable=="string" then echo message and run program else it must quit exit 1 if anything else or even empty.

I have tried so much already and used Google, but I just seem to continually receive syntax errors and unexpected at this time errors, CMD is really a struggle to troubleshoot although it is pretty old and yet powerful.

So what i am trying to understand is what is wrong with my brackets or structure, because it is telling me ( was unexpected or syntax error or was not expected yet... anoying.

My structure like:

echo off
if "%1"=="imre" goto imre (
echo found parameter "%1"
) else if NOT %1=="imre" echo you failed

just not working; I get

( was unexpected

and other syntax errors if I try to tweak this...

All I want is to: (this is what i have written but it is not working)

if %variable%=="hello" (
echo hello is correct 
echo now lets start this program 
start /w excel.exe
) else (
exit 1
)

This does not work, and I don't understand what I am doing wrong. this is CMD Batch which is am trying to achieve

Upvotes: 2

Views: 11402

Answers (2)

Mr. Kite
Mr. Kite

Reputation: 101

Yes, your quotes are inconsistent. But I believe goto is causing the issue, since it comes ahead of the rest of what you want to do.

Try this:

echo off
if "%1"=="imre" (
echo found parameter "%1"
goto imre 
) else if NOT "%1"=="imre" echo you failed

Upvotes: 0

Does your %variable% variable actually contain "hello" or just, hello?

If your parameter doesn't have the quotes, you should use this:

if "%variable%"=="hello" (
    echo hello is correct 
    echo now lets start this program 
    start /w excel.exe
) else (
    exit 1
)

This way if you pass Hello into %variable%, you're actually comparing "hello" == "hello".

Upvotes: 0

Related Questions