只能在聊天批处理文件上写一个单词

时间:2015-03-30 16:00:03

标签: batch-file chat goto

以下代码是我程序的一部分。我的问题是,当我输入我的消息时,如果它的1个单词,它的效果非常好。但如果它更多,它说goto是预期的。需要Q和R选项来刷新聊天并弹出退出提示。

set /p M=Enter Your Message:
if %M%==Q goto B
if %M%==q goto B
if %M%==R goto A
if %M%==r goto A
echo %U%: %M% >>%S%.txt

我该怎么办?

PS。这是我在这里的第一个问题,对不起,如果格式错误的话)

1 个答案:

答案 0 :(得分:1)

如果输入有多个单词,解析器将在变量替换后看到

if this is a test==Q goto B

这是一个无效的命令。

最简单的解决方案是使用引号

if "%M%"=="Q" goto B
....

将被解释为

if "this is a test"=="Q" goto B

已修改以适应评论

这是一个聊天脚本的骨架,其中包含"自动更新"在评论中指出的条款。

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Configuration
    set "chatFile=c:\temp\chat.txt"
    set "lastSize=-1"

:selectOption
    rem Update screen
    call :refresh 

    rem retrieve user selection
    >nul choice /c rqw /n /t 3 /d r /m ""

    rem Check what has been selected
    if errorlevel 3 call :write & goto :selectOption
    if errorlevel 2 goto :quit

    rem Select another option
    goto :selectOption


:refresh [bForceRefresh]
    rem Check if refresh is being forced
    if not "%~1"=="" set "lastSize=-1"

    rem Ensure we have a file
    if not exist "%chatFile%" >"%chatFile%" echo(

    rem Check if we can skip the refresh. 
    rem If file has no changed its size there are no changes in content
    for %%a in ("%chatFile%") do if %%~za leq %lastSize% ( 
        goto :skipRefresh 
    ) else ( 
        set "lastSize=%%~za" 
    )

    rem Paint the screen
    cls
    type "%chatFile%"

    rem Show the options
    echo(
    <nul set /p ".=[R]efresh [Q]uit [W]rite ?"
:skipRefresh
    goto :eof


:write
    setlocal
    rem Ask message
    echo(
    echo(
    set "message="
    set /p "message=What do you want to say? >"

    rem If there is a message, write to output file, 
    rem else force screen refresh to remove the message question
    if defined message (
        setlocal enabledelayedexpansion
        >>"%chatFile%" echo(%time% :[ %username% ]: !message!
        endlocal
    ) else (
        call :refresh forceRefresh
    )
    endlocal 
    goto :eof

:quit
    cls
    exit /b