【问题标题】:Batch Can't Enter Spaces to a Variable批处理不能在变量中输入空格
【发布时间】:2014-01-27 13:53:35
【问题描述】:

为什么我在输入消息时输入空格会崩溃?

代码:

:chatter
cls
echo type /help to see chat commands
echo please use: _ instead of spaces(the problem of this code).
set /p message="enter message: "
if %message%==/help goto chatcmds
if %message%==/exit goto chatcmds
if %message%==/changeuser goto chatcmds
echo [%username%]: %message% >> %joinedchat%.chat
goto chatter

我只是看不出它不起作用的原因。 我尝试了其他人的建议,但它们不起作用。

【问题讨论】:

    标签: file variables batch-file space


    【解决方案1】:

    您需要将 var 和值括在引号中,因为空格是命令分隔符。我按照 cmets 中的建议添加了 /I 开关,以进行不区分大小写的字符串比较。

    if /I "%message%"=="/help" goto chatcmds
    if /I "%message%"=="/exit" goto chatcmds
    if /I "%message%"=="/changeuser" goto chatcmds
    

    【讨论】:

    • /i 选项添加到if 命令可能是个好主意。
    • @David:也可以考虑用双引号将路径/文件名括起来。 (除非您确定您的文件/路径永远不能包含空格和/或特殊字符。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多