2010-09-15 5 views
4

Я разрабатываю чат для своей школьной сетевой сети, он набирает ваши сообщения в DLL-файл, чтобы замаскировать журнал чата.Почему пакетный файл сбой?

Проблема в том, что внезапно, когда я начал набирать сообщения, в которых есть пробел, пакетный файл выходит из строя. Например, если я ввожу сообщение, как «ч» ч партия будет врезаться с ошибкой:

h==exit was unexpected at this time

Heres сценарий:

@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 

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

+3

Я ... просто ... нет слов, чтобы описать это. –

+3

A * пакетный сетевой чат *? Это чертовски остроумно. Если бы у меня было время, я бы скопировал + вставить его и попробовать сам. Было бы +1, но у меня нет голосов, оставшихся на сегодня –

+0

haha..its только с недавнего изменения кода, который я не могу отправить сообщение, в котором есть пробел ... проблема в том, что довольно много и не знаю, что вызвало эту проблему ... я помню то же самое, что было около года назад, когда я сделал еще один сетевой чат ... – Josh

ответ

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 
+0

Чтобы избежать проблем с котическими символами, используйте задержку расширения, чем процентное расширение (если «! Message!» == «exit») работает также с кавычками – jeb

+0

Thx, он работал так хорошо, мой код был «if% info% == Hi! goto hi', он разбился. Теперь код 'if/i"% info% "==" Привет! "'. – MineCMD

Смежные вопросы