为什么批处理文件崩溃?

时间:2010-09-15 19:05:48

标签: batch-file chat

我正在为我的学校局域网设计聊天,它会将您的消息键入.dll文件,以便伪装聊天记录。

问题是突然间,每当我开始输入其中有空格的消息时,批处理文件就会崩溃。例如,如果我将消息输入为“h h”,则批处理将崩溃并显示错误:

  

h ==此时退出意外

继承剧本:

@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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:5)

我猜错误发生在这一行:

IF %message%==exit GOTO :exit

如果%message%包含空格,例如h h,此行扩展为

IF h h==exit GOTO :exit

这不是IF运算符的有效语法。


要避免该错误,请将操作数括在引号中:

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

但请注意,如果%message%包含引号字符",则此变体也不可靠并会引发语法错误。

顺便说一句,您可以使用/i开关执行不区分大小写的字符串比较:

IF /i "%message%" EQU "exit" GOTO :exit