1

Hi I am working on a windows batch file and am trying to get the program to end if the user does not enter a string, but when I run it and do not input anything the whole thing still runs. Any advice would be great thanks.

:: Sets variable studentName to what the user inputs.
set /p studentName=Enter student name: 

::If the user does not input anything go to end option
if "%studentName%"=="" goto end

:: Displays filename, student's entered name, and the random number
echo Usage: %0 %studentName%
echo Hello %studentName%, your secret number is %RANDOM%

:: Pauses screen while user reads secret number
pause

:: Clear screen for user.
cls

echo Hope you remeber that number, %studentName%!


:end
echo Usage: %0 studentName
pause
exit /b
DJezus
  • 311
  • 1
  • 2
  • 11
  • I believe you replace the quotes with brackets, ie: `if "%studentname%"==[] GOTO end` – Taylor Ackley Mar 22 '17 at 23:42
  • 2
    Let me guess: you tested the script first with a name and then without one , and you never closed the command prompt between the tests, right? – SomethingDark Mar 22 '17 at 23:42
  • 3
    @TaylorAckley - Not even remotely true. Using quotes for `if` statements is considered best practice. Also, since both the quotes and the brackets are considered part of the comparison strings, the statement you posted will never be true under any circumstances. – SomethingDark Mar 22 '17 at 23:44
  • Oh jeez.. No i did not close between tests. Thank You @SomethingDark – DJezus Mar 22 '17 at 23:44

2 Answers2

2

When variables are set in a normal batch script, they persist in the environment until they are deleted or the environment is closed. Your problem stems from the fact that you gave %studentName% a value without preparing the environment first. You have two options:

Option 1: Clear the variable before using it

@echo off

:: Clears the value of %studentName%. The quotes are to prevent extra spaces from sneaking onto the end
set "studentName="

:: Sets variable studentName to what the user inputs.
set /p studentName=Enter student name: 

::If the user does not input anything go to end option
if "%studentName%"=="" goto end

:: Displays filename, student's entered name, and the random number
echo Usage: %0 %studentName%
echo Hello %studentName%, your secret number is %RANDOM%

:: Pauses screen while user reads secret number
pause

:: Clear screen for user.
cls

echo Hope you remeber that number, %studentName%!


:end
echo Usage: %0 studentName
pause
exit /b

Pros:

  • If you need other variables to persist, you can run this over and over and they will stay until the command prompt is closed.

Cons:

  • If you have a lot of variables that need to not persist, you need to clear each one manually.

Option 2: Using setlocal to create a new environment

@echo off
setlocal

:: Sets variable studentName to what the user inputs.
set /p studentName=Enter student name: 

::If the user does not input anything go to end option
if "%studentName%"=="" goto end

:: Displays filename, student's entered name, and the random number
echo Usage: %0 %studentName%
echo Hello %studentName%, your secret number is %RANDOM%

:: Pauses screen while user reads secret number
pause

:: Clear screen for user.
cls

echo Hope you remeber that number, %studentName%!


:end
echo Usage: %0 studentName
pause
exit /b

Pros:

  • If you have a lot of variables to clear, this will save a ton of typing.
  • If When you need to use delayed expansion, you'll need to use this method anyway

Cons:

  • Variable values won't persist across multiple runs unless you store them somewhere
Community
  • 1
  • 1
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
1
set /p studentName=Enter student name: || goto :end

With command extensions enabled (the default configuration, or can be enabled with setlocal enableextensions) the conditional operator || (execute next command if the previous failed) will catch the failure (no input) of the set command to retrieve data.

MC ND
  • 69,615
  • 8
  • 84
  • 126