Why does the batch file fail?

I am developing a chat for my school network, it types your messages into a DLL file to mask the chat log.

The problem is that all of a sudden, when I started typing messages that contain a space, the batch file crashes. For example, if I enter the message as "hh", the packet will fail with an error:

h == exit was unexpected now

Here's the script:

@echo off CLS COLOR f2 SET user=%username% SET message= IF %user%==Josh SET cuser=Miltzi & GOTO :admin IF %user%==miltzj SET cuser=Miltzi & GOTO :admin IF %user%==steinj SET cuser=Jarod & GOTO :first IF %user%==steinda SET cuser=Daniel & GOTO :first IF %user%==rubine SET cuser=Evan & GOTO :first IF %user%==sklairn SET cuser=Nathan & GOTO :first IF %user%==portnoyc SET cuser=Craig & GOTO :first IF %user%==polakowa SET cuser=Polly & GOTO :first IF %user%==selbya SET cuser=Alex & GOTO :first IF %user%==vanderwesthuizenl SET cuser=Lance & GOTO :first msg * This is a test message! :D REM the above line is incase a teacher runs the chat remotely from their computer exit :CHAT TITLE Grade 11 IT chat :) IF NOT EXIST C:\users\Josh\desktop\1.dll echo Chat cleared. >> C:\users\Josh\desktop\1.dll CLS type C:\users\Josh\desktop\1.dll SET /P message=Type message and press enter (Type help to view chat options): IF ERRORLEVEL 1 GOTO :CHAT IF %message%==exit GOTO :exit IF %message%==Exit GOTO :exit IF %message%==EXIT GOTO :exit IF %message%=="exit" GOTO :exit IF %message%==help GOTO :help IF %message%==Help GOTO :help IF %message%=="help" GOTO :help echo %user%: %message% >> C:\users\Josh\desktop\1.dll GOTO :CHAT :exit CLS echo %user% left the chat. >> C:\users\Josh\desktop\1.dll exit :help CLS echo Welcome to the help section echo To exit the chat, please type exit as a message into the chat rather than closing the cmd box manually. echo To refresh the chats messages, just press enter without writing any text. echo Please press enter to go back to the chat :) pause GOTO :CHAT :ACHAT TITLE Grade 11 IT chat :) IF NOT EXIST C:\users\Josh\desktop\1.dll echo Chat cleared. >> C:\users\Josh\desktop\1.dll CLS type C:\users\Josh\desktop\1.dll SET /P message=Type message and press enter (Type help to view chat options): IF ERRORLEVEL 1 GOTO :ACHAT IF %message%==exit GOTO :exit IF %message%==Exit GOTO :exit IF %message%==EXIT GOTO :exit IF %message%=="exit" GOTO :exit IF %message%==help GOTO :help IF %message%==Help GOTO :help IF %message%=="help" GOTO :help IF %message%==cls GOTO :CLS echo %user%: %message% >> C:\users\Josh\desktop\1.dll GOTO :CHAT :exit CLS echo %user% left the chat. >> C:\users\Josh\desktop\1.dll exit :help CLS echo Welcome to the help section echo To exit the chat, please type exit as a message into the chat rather than closing the cmd box manually. echo To refresh the chats messages, just press enter without writing any text. echo Please press enter to go back to the chat :) pause GOTO :CHAT :CLS del C:\users\Josh\desktop\1.dll GOTO :ACHAT :admin echo %user% joined the chat. >> C:\users\Josh\desktop\1.dll GOTO :ACHAT :first echo %user% joined the chat. >> C:\users\Josh\desktop\1.dll GOTO :CHAT exit 

Any help would be appreciated!

+4
source share
1 answer

I think an error occurs on this line:

 IF %message%==exit GOTO :exit 

If %message% contains a space, for example. hh , this line extends to

 IF hh==exit GOTO :exit 

which is not a valid syntax for an IF .


To avoid errors, enclose the operands in quotation marks:

 IF "%message%"=="exit" GOTO :exit 

But keep in mind that this option is also unreliable and will cause a syntax error if %message% contains a quotation mark. "

And by the way, you can compare case insensitive strings with the /i switch:

 IF /i "%message%" EQU "exit" GOTO :exit 
+5
source

All Articles