Почему пакетный файл вылетает?

Я разрабатываю чат для своей школьной локальной сети, он вводит ваши сообщения в файл .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

Любая помощь будет оценена по достоинству!


person Josh    schedule 15.09.2010    source источник
comment
У меня... просто... нет слов, чтобы описать это.   -  person Justin Garrick    schedule 15.09.2010
comment
Групповой сетевой чат? Это чертовски гениально. Если бы у меня было время, я бы скопировал + вставил и попробовал сам. Хотел бы +1, но на сегодня у меня не осталось голосов   -  person Pekka    schedule 15.09.2010
comment
ха-ха... только после недавней модификации кода я не могу отправить сообщение, в котором есть пробел... проблема в том, что я довольно сильно изменился и понятия не имею, что вызвало эту проблему... я помню то же самое это случилось около года назад, когда я сделал еще один сетевой чат...   -  person Josh    schedule 15.09.2010
comment
он сделал то же самое ... sum1, пожалуйста, помогите!   -  person Josh    schedule 15.09.2010


Ответы (1)


Я предполагаю, что ошибка возникает в этой строке:

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
person Helen    schedule 15.09.2010
comment
Чтобы избежать проблем с символами кавычек, используйте отложенное расширение, чем процентное расширение (если !message!==exit) также работает с кавычками. - person jeb; 14.11.2010
comment
Спасибо, это сработало так хорошо, мой код был if %info%==Hi! goto hi, он разбился. Теперь код if /i "%info%"=="Hi!". - person MineCMD; 07.12.2014