gpt4 book ai didi

batch-file - 为什么批处理文件崩溃?

转载 作者:行者123 更新时间:2023-12-02 07:40:08 28 4
gpt4 key购买 nike

我正在为我的学校 LAN 网络设计一个聊天程序,它将您的消息输入到 .dll 文件中,以隐藏聊天日志。

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

h==exit was unexpected at this time

脚本如下:

@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

如有任何帮助,我们将不胜感激!

最佳答案

我猜错误发生在这一行:

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

关于batch-file - 为什么批处理文件崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3720906/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com