0

I'm currently trying to create an installer batch file. At the moment the code is just me trying to work out how the IF statements work. I have not included the code for the installers and such.

I do not know where I am going wrong. When I run the script below, I enter some random text in the hopes that it doesn't reach the code of the first IF statement, but it does. My first IF statement is to check if it is an IP. I typed in "test" but it still runs the code within the first IF statement. It will goto "INSTALL" and then run the code below.

@echo off
:START
SET /P asset=APPLICATION INSTALLER. Please enter IP Address or Asset Number: 

CALL IF "%asset:~2,2%"=="2." (
ECHO IP
GOTO INSTALL
) ELSE (
ECHO Selection is not an IP.
GOTO CASSET
)

:CASSET
CALL IF /I NOT "%asset:~0,5%"=="C-" (
ECHO ASSET
SET asset="C-%asset%"
GOTO INSTALL
)

:INSTALL
echo %asset%
cmd /k
pause

:STOP
ECHO Invalid IP or Asset number provided.
pause

What am I doing wrong? Can someone please explain my obvious mistake?

1
  • 2
    Remove call in front of if!
    – aschipfl
    Jun 13, 2019 at 13:09

1 Answer 1

0

What @aschipfl said in their comment (remove the CALLs in front of your IF statements), but there's also a lot of unnecessary GOTOs and ECHOs that don't appear to really do much as-is (:START, :STOP, your second GOTO INSTALL, the ECHO lines with no variable that just say a single word, etc.).

Another thing I usually find helpful is to put the ) ELSE ( statement on the end of the last line of your IF block like this:

IF "%asset:~2,2%"=="2." (
    ECHO IP
    GOTO INSTALL ) ELSE (
    ECHO Selection is not an IP.
    GOTO CASSET
)

It can help you avoid some headaches when you're nesting statements, since ELSE is not a command by itself - it's just an option for the IF. I also recommend using Tab to help identify levels in your nested statements, though that's personal preference. If you update your question with more specifics on what you're trying to accomplish we'd be happy to help you with the other stuff, and I'll add more to the answer.

Reference: IF statements

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .