Home > Back-end >  "( was unexpected at this time" with if statement
"( was unexpected at this time" with if statement

Time:08-01

I am new to batch and making my first batch project with custom commands using doskey, mainly for myself. I have been successful with all of my scripts in my project up until this point. I have a command that tells the user all the commands (5 so far), much like the help command, except for some reason I am getting errors (described in the title). I might have an idea where the error is coming from, but I have no idea how to fix it. I have checked multiple sources but none of them are even close to what I need to fix, so here is my code:

@echo off
IF "%~1" == "" (
    echo For more information on a specific command, type CMDS command-name
    echo ADD            Adds specified NUM amount to variable
    echo NUM            Sets the NUM amount to a specified value
    echo RESET          Sets variable to 0
    echo SETNUM         Sets variable to a specified value
    echo SUB            Subtracts specified NUM amount from variable
)
IF /I %1 == ADD (
    echo The ADD command will add the amount specified from the NUM command to a variable specified as num
)
IF /I %1 == NUM (
    echo The NUM command will set the variable specified as num to a specific value
)
IF /I %1 == RESET (
    echo The RESET command will reset the variable specified as num to 0
)
IF /I %1 == SETNUM (
    echo The SETNUM command will set the amount that gets added to/subtracted from the variable specified as num to a specific value
)
IF /I %1 == SUB (
    echo The SUB command will subtract the amount specified from the NUM command from a variable specified as num
)
IF not /I %1 == ADD (
    IF not /I %1 == NUM (
        IF not /I %1 == RESET (
            IF not /I %1 == SETNUM (
                IF not /I %1 == SUB (
                    echo %1 is not a command! //I think the error is coming from one of the if statements in these nested ifs, because this is the only thing that doesn't work
                )
            )
        )
    )
)
@echo on

Any help would be appreciated.

CodePudding user response:

Batch is a really, really broken "programming language".

If you do not provide an argument, consider what happens here:

IF /I %1 == ADD (
    echo The ADD command will add the amount specified from the NUM command to a variable specified as num
)

The %1 is replaced with nothing. Which results in:

IF /I  == ADD (
    echo The ADD command will add the amount specified from the NUM command to a variable specified as num
)

The solution is to always use "%1" == "something".

And as Compo pointed out, it's IF /I NOT.

Maybe don't compulsively put @echo off in your file if you're trying to debug it... It would have shown what the problem is.

  • Related