1

Here is the batch script I am working on:

@echo off

:programBEGIN
set FILE=R3D-%input1%.txt
mode con:cols=24 lines=25
color F

set /p input1=}} INPUT: 
cls
if %input1%=="" goto ERR1
goto programLIST

:programLIST
echo Hello

:ERR1
echo Test
pause

Whenever I attempt to run the bat file and input no value, it closes with a quick prompt of "goto was unexpected at the time"

Is my formatting of the if statement correct, or is that the reason it keeps closing?

BunnyFox
  • 11
  • 2
  • 1
    Possible duplicate of [Why is processing of my batch file exited with a syntax error on string comparsion with IF command?](https://stackoverflow.com/questions/34690602/why-is-processing-of-my-batch-file-exited-with-a-syntax-error-on-string-comparsi) – Mofi Oct 06 '17 at 10:59

2 Answers2

4

Because if input is not defined the script looks like this:

if =="" goto ERR1

Which is a syntax error.

Try like this:

if "%input1%"=="" goto ERR1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
-1

Instead of using that if statement, try this instead:

IF DEFINED input1 goto ERR1

If you must use the normal if statement, you can try this:

IF "%input1%"=="" goto ERR1

just to make sure. Other than that, I don't see where the problem would stem from.

  • 1
    Should be `if defined input1 goto ERR1` – DodgyCodeException Oct 06 '17 at 07:08
  • 1
    Your `if defined` is incorrect. It *should* be `if not defined input1 ...`. As posted, the envvar `""` would be interrogated (with empty `input1`) or if `input1` contained `fred` then `"fred"` (including the quotes) would be interrogated. Attempting to set a quoted envvar will result in a value report for that variable name and no change to the variable. (hmm... this bears investigation...) – Magoo Oct 06 '17 at 07:13