How to prevent batch file from terminating

Whenever I write a batch script that provides options using 'if' statements, and input a semicolon, it terminates unexpectedly. Is there a way to prevent this?

For example:
Code:
@echo off

:main
cls
echo Type 'p' or '1' to print a message
echo.
set /p ch=
    if /I %ch%==p (
        cls
        echo Hello
        echo.
        pause
        goto main
    ) 
    if %ch%==1 (
        cls
        echo Hello again
        echo.
        pause
        goto main
    ) else (
        cls
        echo '%ch%' is not a valid parameter
        echo.
        pause
        goto main
    )
If I type in ';' as input, the batch file will close without prompting me that '%ch% is not a valid parameter'.

I tried calling the batch file from within command prompt and it gives me this error:
Code:
Type 'p' or '1' to print a message

;
( was unexpected at this time.

C:\>
Is there a way I can write the batch file so that it doesn't terminate when I have a semicolon in the input?
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Interesting. That's probably a bug. The error message suggests that your compound statement delimiter '(' was interpreted as part of the IF statement condition.

I played around with your script sample and the following throws a different (though no less useless) error message:

if /I %ch%==p
(
cls
echo Hello
echo.
pause
goto main
)

When it's on the next line, the parenthesis is apparently no longer in danger of becoming misinterpreted as part of the condition. I suspect that semi-colons may be getting handled differently due to their special status in path variables.

I'd suggest lodging this with MS.
 

My Computer

Hi Frank,

Basically, certain characters are known as 'delimiters', and that is why you are getting an error. To avoid this, you need to use quotation marks around the variables i.e. "%ch%" instead of %ch% as shown in the following code:
Code:
:main
cls
echo Type 'a', 'b', or 'c' to print a message or 'exit' to exit
echo.
set /p ch=
echo %ch%
echo.
pause
if /I "%ch%"=="exit" goto exit
if /I "%ch%"=="a" (
cls
echo Hello
echo.
pause
goto main )
if /I "%ch%"=="b" (
cls
echo Hello Again
echo.
pause
goto main )
if /I "%ch%"=="c" (
cls
echo And Hello Yet Again
echo.
pause
goto main )
cls
echo %ch% Is Not a Valid Character
echo.
pause
goto main
:exit
cls
echo Exiting
echo.
pause
 
Last edited:

My Computer

System One

  • Manufacturer/Model
    Dwarf Dwf/11/2012 r09/2013
    CPU
    Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.2GHz)
    Motherboard
    ASRock Z77 Extreme4-M
    Memory
    4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
    Graphics Card(s)
    MSI GeForce GTX770 Gaming OC 2GB
    Sound Card
    Realtek High Definition on board solution (ALC 898)
    Monitor(s) Displays
    ViewSonic VA1912w Widescreen
    Screen Resolution
    1440x900
    Hard Drives
    OCZ Agility 3 120GB SATA III x2 (RAID 0)
    Samsung HD501LJ 500GB SATA II x2
    Hitachi HDS721010CLA332 1TB SATA II
    Iomega 1.5TB Ext USB 2.0
    WD 2.0TB Ext USB 3.0
    PSU
    XFX Pro Series 850W Semi-Modular
    Case
    Gigabyte IF233
    Cooling
    1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
    Keyboard
    Microsoft Comfort Curve Keyboard 3000 (USB)
    Mouse
    Microsoft Comfort Mouse 3000 for Business (USB)
    Internet Speed
    NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
    Other Info
    Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
    Lexmark S305 Printer/Scanner/Copier (USB)
    WEI Score: 8.1/8.1/8.5/8.5/8.25
    Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
Wow thanks a lot for that. I was reading a tutorial on batch and read that a semicolon could be used for comments so I just guessed that even as input, the semicolon would act as a comment and the script would just continue to the next line.

I was also wondering, if I have a blank line between 2 commands, would the script without the space run faster?

For example:
Code:
@echo off
taskkill /f /im explorer.exe
Code:
@echo off

taskkill /f /im explorer.exe
Would the first script run faster than the second, or does it not make a difference?
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
It makes absolutely no difference whatsoever to the speed that a script executes, the only difference is that it might be easier to read and debug.
 

My Computer

System One

  • Manufacturer/Model
    Dwarf Dwf/11/2012 r09/2013
    CPU
    Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.2GHz)
    Motherboard
    ASRock Z77 Extreme4-M
    Memory
    4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
    Graphics Card(s)
    MSI GeForce GTX770 Gaming OC 2GB
    Sound Card
    Realtek High Definition on board solution (ALC 898)
    Monitor(s) Displays
    ViewSonic VA1912w Widescreen
    Screen Resolution
    1440x900
    Hard Drives
    OCZ Agility 3 120GB SATA III x2 (RAID 0)
    Samsung HD501LJ 500GB SATA II x2
    Hitachi HDS721010CLA332 1TB SATA II
    Iomega 1.5TB Ext USB 2.0
    WD 2.0TB Ext USB 3.0
    PSU
    XFX Pro Series 850W Semi-Modular
    Case
    Gigabyte IF233
    Cooling
    1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
    Keyboard
    Microsoft Comfort Curve Keyboard 3000 (USB)
    Mouse
    Microsoft Comfort Mouse 3000 for Business (USB)
    Internet Speed
    NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
    Other Info
    Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
    Lexmark S305 Printer/Scanner/Copier (USB)
    WEI Score: 8.1/8.1/8.5/8.5/8.25
    Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
Hmm, it seems that typing double quotes as input, even after applying the fix you suggested, causes the script to crash.

Nothing to worry about I guess, seeing as it's harder to accidentally type " than it is to type ;.
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Nice one Dwarf. I didn't realise the command shell could allow escape out of the conditional processing in that manner. Rep time.
 

My Computer

Hmm, it seems that typing double quotes as input, even after applying the fix you suggested, causes the script to crash.

Nothing to worry about I guess, seeing as it's harder to accidentally type " than it is to type ;.
You're welcome, Frank. Yes, this does seem to be a limitation of the relatively simple and limited facilities of the batch file scripting.
 

My Computer

System One

  • Manufacturer/Model
    Dwarf Dwf/11/2012 r09/2013
    CPU
    Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.2GHz)
    Motherboard
    ASRock Z77 Extreme4-M
    Memory
    4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
    Graphics Card(s)
    MSI GeForce GTX770 Gaming OC 2GB
    Sound Card
    Realtek High Definition on board solution (ALC 898)
    Monitor(s) Displays
    ViewSonic VA1912w Widescreen
    Screen Resolution
    1440x900
    Hard Drives
    OCZ Agility 3 120GB SATA III x2 (RAID 0)
    Samsung HD501LJ 500GB SATA II x2
    Hitachi HDS721010CLA332 1TB SATA II
    Iomega 1.5TB Ext USB 2.0
    WD 2.0TB Ext USB 3.0
    PSU
    XFX Pro Series 850W Semi-Modular
    Case
    Gigabyte IF233
    Cooling
    1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
    Keyboard
    Microsoft Comfort Curve Keyboard 3000 (USB)
    Mouse
    Microsoft Comfort Mouse 3000 for Business (USB)
    Internet Speed
    NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
    Other Info
    Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
    Lexmark S305 Printer/Scanner/Copier (USB)
    WEI Score: 8.1/8.1/8.5/8.5/8.25
    Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
I seem to have found a solution to my second problem (explained in post #6). I'm adding the reply to this thread so that the solution's in context.

Here's an interactive example you can try out:
Code:
@echo off

:main
cls
echo                 Main Menu
echo.
echo (0) Type me
echo (1) Or me
echo.
echo (e) Exit
echo.
set mn=null
set /p mn=
set mn=%mn:"=null%
    if "%mn%"=="0" (
        cls
        echo Hello
        echo.
        pause
        goto main
    )
    if "%mn%"=="1" (
        cls
        echo Hello again
        echo.
        pause
        goto main
    )
    if /I "%mn%"=="e" (
        exit
    ) else (
        cls
        echo Not a valid parameter
        echo.
        pause
        goto main
    )
Basically what I did was before processing the input, search it for any quotation marks (") and convert them to something acceptable; "null" in this case. This is the line that does it:
Code:
set mn=%mn:"=null%
What it does is make the variable equal to itself substituting what's after the colon for what's after the equal sign, in this case, a quotation mark for 'null' (you can make 'null' anything you want, I just set it as null so I remember what its purpose is).

So for example, if your input was ' 1" ' (without the enclosing quotation marks), it would become ' 1null ' and then be tested against the if statements.

The reason I have this line:
Code:
set mn=null
before assigning user input to the variable is so that if the user types '1' and it loops back, the variable will be reset to 'null'. If I didn't reset it and hit enter without any input, the old value of '1' would remain and it would execute the same commands again.

Hope this helps.
 

My Computer

System One

  • Manufacturer/Model
    Acer Aspire 5630
    CPU
    Intel Core 2 Duo T5500 1.66GHz 65nm 667MHz FSB 2MB L2
    Motherboard
    Acer Grapevine Intel i945GM Chipset
    Memory
    2x1GB DDR2 333MHz
    Graphics Card(s)
    Intel Integrated GMA 950 256MB
    Sound Card
    Realtek High Definition Audio
    Monitor(s) Displays
    15.4" WXGA Acer CrystalBrite LCD 16ms
    Screen Resolution
    1280x800
    Hard Drives
    200GB 5400rpm Toshiba MK2035GSS ATA-7, 250GB 7200rpm Maxtor External USB Hard Drive
Back
Top