Batch file terminates unexpectedly when enter key is pressed
-
21-12-2019 - |
Question
I am currently writing a batch file to get users to enter the telephone's serial number. I have written the following:
set /p sernumoftel1="Enter Serial Number of the First Telephone: "
set /p sernumoftel2="Enter Serial Number of the Second Telephone (Press Enter Key To Skip If You DO NOT HAVE A Second Telephone): "
if not %sernumoftel1% == "" (
echo. > soe.txt
echo Serial Number of Telephone 1: >> soe.txt
echo %sernumoftel1% >> soe.txt
)
if not %sernumoftel2% == "" (
echo. >> soe.txt
echo Serial Number of Telephone 2: >> soe.txt
echo %sernumoftel2% >> soe.txt
)
echo You Are Done!
pause
However, at the second question, when I pressed the enter key, command prompt exited unexpectedly, without displaying 'You Are Done!'.
What did I do wrong? Any help is appericiated.
Solution
Use quotes on if
statements
if not "%sernumoftel1%" == "" (....
if not "%sernumoftel2%" == "" (...
OTHER TIPS
You can also look into making sure the variable is defined after set /p.
if not defined variable (
echo Error: Variable empty!
pause && exit
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow