views:

48

answers:

1

I'm designing a chat for my school LAN network, it types your messages into a .dll file so as to disguise the chat log.

The problem is that all of a sudden, whenever I started typing messages which have a space in them, the batch file crashes. For example if I enter the message as "h h" the batch will crash with the error:

h==exit was unexpected at this time

Heres 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!

+3  A: 

I guess the error occurs in this line:

IF %message%==exit GOTO :exit

If %message% includes a space, e.g. h h, this line expands into

IF h h==exit GOTO :exit

which is not a valid syntax for the IF operator.


To avoid the error, enclose the operands in quotes:

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

But be aware that this variant is also not reliable and will throw a syntax error if %message% includes the quote character ".

And by the way, you can perform case-insensitive string comparison using the /i switch:

IF /i "%message%" EQU "exit" GOTO :exit
Helen
Awesome thanks so much, it all works now :)
Josh
@Josh Glad I could help. :) Remember to mark an answer as accepted if it solved your problem. (See [How does accepting an answer work?](http://meta.stackoverflow.com/q/5234/131247)) Thanks :)
Helen