GuerrillaXceL
GuerrillaXceL

Reputation: 3

I need help on how to stop this batch-file from closing when an illegal character is entered by the user

I'm pretty much a novice and need help on how to stop this batch-file from closing when any illegal character is entered by the user...

For example, when a users input is ";" or "&&" and the input basically crashes the batch-file.

My Code:

@echo off
cls
:start
taskkill /f /im process1.exe
taskkill /f /im process2.exe
taskkill /f /im process3.exe
taskkill /f /im process4.exe
taskkill /f /im process5.exe
:restart
set choice=
set /p choice="Do you want to run these batch commands again? Type 'y' and press 'Enter' to re-execute the batch commands again or type 'n' and press 'Enter' to terminate: "
if not '%choice%'=='n' if not '%choice%'=='y' goto restart
if '%choice%'=='n' set choice=%choice:~0,1%
if '%choice%'=='y' goto start
:end

Thanks!

Upvotes: 0

Views: 50

Answers (1)

DavidPostill
DavidPostill

Reputation: 7921

How do I stop this batch-file from closing when any illegal character is entered?

Option 1:

Write your if expressions using " instead of backticks:

if not "%choice%"=="n" if not "%choice%"=="y" goto restart
if "%choice%"=="n" set choice=%choice:~0,1%
if "%choice%"=="y" goto start

Option 2 (better):

Use choice:

choice /c yn /m "Do you want to run these batch commands again? Press [y] or [n]
if errorlevel 2 goto :exit
if errorlevel 1 goto :restart

Further Reading

Upvotes: 1

Related Questions