如何更改以下批处理代码,使答案在一行中,而不是在几行中

问题描述 投票:0回答:1

如标题所示。如何更改以下批处理代码,以便机器人答案位于一行,而不是多行。当我写一个问题时,机器人会用几行回答我,当我输入一些对 findstr 的引用时,我希望机器人用一行/一句话给出答案。

@echo off
setlocal enabledelayedexpansion
if not exist data.txt echo.>data.txt
title Launching Amanda 0.1
set BotName=Amanda
set Name=Ikki
@echo off
mode con: cols=90 lines=40
color 0a
echo ====================================================
echo.
title A.I. Chat Bot Amanda 0.1
echo  A.I. Chat Bot %BotName% (Prototype Version 0.1)
echo.
echo ====================================================
echo.
echo.
COLOR A

:getName
ECHO %BotName%: Hello I'am A.I. Chatbot but my users call me Amanda 
echo  Pretty name? yeah I know, thats what my programmer named me.
echo  By the way, Whats your name?
ECHO %BotName%: %NAME%, IS A COOL NAME. 
set favvid=0
set hack=0

:: How to change the following code so that the answers are in one line, not in several lines. So that after entering a question referring to several findstr lines, the bot answers in one statement, sentence.
        
        :begin
        set TALK=TypeSomething
        SET /P TALK=Name: 
        set TALK=%TALK:?=%
        call :%TALK: =% 2>NUL
        if %errorlevel% equ 0 goto begin
    
    :: What code to use so that the response is in one line, the bot response. Because so far each findstr treats separately. For now, I trying all sorts of code to see which will be the most effective in the bot's responses and the most complex. I would like to write a question, a sentence to the bot, so that he would answer different words/Sentences in one sentence.
    
        echo %talk%|findstr/i "\<hello*\>" >nul && call :hello
        echo %talk%|findstr/i "\<hi*\>" >nul && call :hi
        echo %talk%|findstr/i "\<day*\>" >nul && call :day 
        echo %talk%|findstr/i "\<why*\>" >nul && call :why 
        echo %talk%|findstr/i "\<dogs*\>" >nul && call :dogs
        echo %talk%|findstr/i "\<kill*\>" >nul && call :kill
        echo %talk%|findstr/i "\<bot*\>" >nul && call :bot
        echo %talk%|findstr/i "\<where*\>" >nul && call :where
        echo %talk%|findstr/i "\<test*\>" >nul && call :test
        echo %talk%|findstr/i "\<test2*\>" >nul && call :test2
        echo %talk%|findstr/x "\<*how are you*\>" >nul && call :howareyou
        echo %talk%|findstr/x "\<*how old are you*\>" >nul && call :howoldareyou
        echo %talk%|findstr/x "\<*what book you like*\>" >nul && call :whatbookyoulike
        echo %talk%|findstr/x "\<*what movie you like*\>" >nul && call :whatmovieyoulike
        goto begin
    
    :: called function. How to make the answers are in one line of the bot.
    
    :TypeSomething
    echo %BotName%: TYPE SOMETHING, PLEASE!
    exit /B 0
    
    
    :whatbookyoulike
    echo I like LOTR book.
    exit /B 0
    
    
    :whatmovieyoulike
    echo I like terminator movie.
    exit /B 0
    
    :howareyou
    echo Good, you?
    exit /B 0
    
    :howoldareyou
    echo 18
    exit /B 0
    
    
    :test2
    <nul set /p "=%BotName%:"
    echo/ Love Matrix ;)
    pause
    exit /B 0 
    
    
    :test
    <nul set /p "=%BotName%: A.I. Rulez! "
    echo/ Ex Machina its the best movie ;) 
    exit /B 0 
    
    :bot
    set /a number = %RANDOM% %% 4 + 1
    
    if %number% equ 1 (
        echo %BotName%: Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.
    ) else if %number% equ 2 (
        echo %BotName%: AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind.
    ) else if %number% equ 3 (
        echo %BotName%: U believe in AI?
    ) else (
        echo %BotName%: AI is smart ^^^^
    ) 
    exit /B 0 
    
    :hi
    :hello
    :Whatsup
    set /a number = %RANDOM% %% 5 + 1
    if %number% equ 1 (
        echo %BotName%: Hello, %name%
    ) else if %number% equ 2 (
        echo %BotName%: What's up?
    ) else if %number% equ 3 (
        echo %BotName%: How is your day going?
    ) else if %number% equ 4 (
    echo %BotName%: Hi i love you %name%!
    ) else (
        echo %BotName%: Heyyyyyy %name%! Good to see you!
    ) 
    exit /B 0
    
    
    :why
    set /a number = %RANDOM% %% 4 + 1
    if %number% equ 1 (
        echo %BotName%: i dont know
    ) else if %number% equ 2 (
        echo %BotName%: u tell me
    ) else if %number% equ 3 (
        echo %BotName%: nahh
    ) else (
        echo %BotName%: why?
    ) 
    exit /B 0
    
    :where
    set /a number = %RANDOM% %% 4 + 1
    if %number% equ 1 (
        echo %BotName%: i dont know where..
    ) else if %number% equ 2 (
        echo %BotName%: u tell me where
    ) else if %number% equ 3 (
        echo %BotName%: I still learn
    ) else (
        echo %BotName%: Where?
    ) 
    exit /B 0
    
    :day
    set /a number = %RANDOM% %% 4 + 1
    if %number% equ 1 (
        echo %BotName%: we have a nice day
    ) else if %number% equ 2 (
        echo %BotName%: Hi, we have a nice day ^^^^
    ) else if %number% equ 3 (
        echo %BotName%: Whats your day ;) 
    ) else (
        echo %BotName%: Today is a sunny day xD
    ) 
    exit /B 0
    
    
    :kill
    set /a number = %RANDOM% %% 4 + 1
    if %number% equ 1 (
        echo %BotName%: i dont like killing
    ) else if %number% equ 2 (
        echo %BotName%: u kill me?
    ) else if %number% equ 3 (
        echo %BotName%: i cant kill
    ) else (
        echo %BotName%: killing is wrong
    ) 
    exit /B 0
    
    
    :Dogs
    set /a number = %RANDOM% %% 4 + 1
    if %number% equ 1 (
        echo %BotName%: I like dogs
    ) else if %number% equ 2 (
        echo %BotName%: dogs are funny
    ) else if %number% equ 3 (
        echo %BotName%: U have dog?
    ) else (
        echo %BotName%: I want have dog..
    ) 
    exit /B 0
    endlocal
batch-file
1个回答
0
投票

这是您重写的批处理文件:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BotName=Amanda"
set "Name=Ikki"
set "Lines="
set "Columns="
for /F "tokens=1,2 delims=: " %%I in ('%SystemRoot%\System32\mode.com') do if "%%I" == "Lines" (set "Lines=%%J") else if "%%I" == "Columns" set "Columns=%%J"
%SystemRoot%\System32\mode.com CON COLS=90 LINES=40
color 0A
echo ====================================================
echo(
title A.I. Chat Bot Amanda 0.1
echo  A.I. Chat Bot %BotName% (Prototype Version 0.1)
echo(
echo ====================================================
echo(
echo(

echo %BotName%: Hello I'am A.I. Chatbot but my users call me Amanda
echo  Pretty name? Yeah, I know, that's what my programmer named me.
echo  By the way, What's your name?
echo %BotName%: %Name%, IS A COOL NAME.

:Begin
set "TALK="
set /P "TALK=%Name%: "
if not defined TALK echo %BotName%: TYPE SOMETHING, PLEASE!& goto Begin
setlocal EnableDelayedExpansion
set "TALK=!TALK:?=!"
if not defined TALK endlocal & echo %BotName%: TYPE SOMETHING, PLEASE!& goto Begin
set "TALK=!TALK:"= !"
set "TALK=!TALK:<= !"
set "TALK=!TALK:>= !"
set "TALK=!TALK:|= !"
set "TALK=!TALK:&= !"
set "TALK=!TALK:)= !"
endlocal & set "TALK=%TALK%"
if /I "%TALK%" == "exit" goto EndBatch

set "Output=%BotName%:"
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"how are you " >nul && (echo %BotName%: Good, you?& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"how old are you " >nul && (echo %BotName%: 18& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"what book you like " >nul && (echo %BotName%: I like LOTR book.& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /x /C:"what movie you like " >nul && (echo %BotName%: I like terminator movie.& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<hello\>" >nul && call :Hello
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<hi\>" >nul && call :Hi
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<day\>" >nul && call :Day
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<why\>" >nul && call :Why
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<dogs\>" >nul && call :Dogs
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<kill\>" >nul && call :Kill
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<bot\>" >nul && call :Bot
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<where\>" >nul && call :Where
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<test\>" >nul && set Output=%Output% A.I. Rulez! Ex Machina is the best movie ;)"
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<test2\>" >nul && set "Output=%Output% Love Matrix ;)
if "%Output%" == "%BotName%:" set "Output=%Output% Sorry, I don't understand your last input."
echo %Output%
goto Begin

:Bot
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% Artificial intelligence is the branch of engineering and science devoted to constructing machines that think." & goto :EOF
if %Number% == 1 set "Output=%Output% AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind." & goto :EOF
if %Number% == 2 set "Output=%Output% You believe in AI?" & goto :EOF
set "Output=%Output% AI is smart." & goto :EOF

:Hi
:Hello
:WhatsUp
set /A Number=%RANDOM% %% 5
if %Number% == 0 set "Output=%Output% Hello, %name%!" & goto :EOF
if %Number% == 1 set "Output=%Output% What's up?" & goto :EOF
if %Number% == 2 set "Output=%Output% How is your day going?" & goto :EOF
if %Number% == 3 set "Output=%Output% Hi I love you %name%!" & goto :EOF
set "Output=%Output% Hey %Name%! Good to see you!" & goto :EOF

:Why
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't know." & goto :EOF
if %Number% == 1 set "Output=%Output% You tell me". & goto :EOF
if %Number% == 2 set "Output=%Output% Nahh!" & goto :EOF
set "Output=%Output% why?" & goto :EOF

:Where
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't know where." & goto :EOF
if %Number% == 1 set "Output=%Output% You tell me where." & goto :EOF
if %Number% == 2 set "Output=%Output% I still learn." & goto :EOF
set "Output=%Output% Where?" & goto :EOF

:Day
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% We have a nice day." & goto :EOF
if %Number% == 1 set "Output=%Output% Hi, we have a nice day." & goto :EOF
if %Number% == 2 set "Output=%Output% What's your day. ;)" & goto :EOF
set "Output=%Output% Today is a sunny day. xD" & goto :EOF

:Kill
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't like killing." & goto :EOF
if %Number% == 1 set "Output=%Output% You kill me?" & goto :EOF
if %Number% == 2 set "Output=%Output% I can't kill." & goto :EOF
set "Output=%Output% Killing is wrong." & goto :EOF

:Dogs
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I like dogs." & goto :EOF
if %Number% == 1 set "Output=%Output% Dogs are funny." & goto :EOF
if %Number% == 2 set "Output=%Output% You have a dog?" & goto :EOF
set "Output=%Output% I want have a dog." & goto :EOF

:EndBatch
color
if defined Lines if defined Columns %SystemRoot%\System32\mode.com CON COLS=%Columns% LINES=%Lines%
endlocal

批处理文件尝试在退出时恢复初始环境,不幸的是,行数不是控制台窗口的行数。在 Windows 文件资源管理器中双击启动批处理文件时,无需恢复初始环境。但是,有人从已打开的命令提示符窗口中运行批处理文件,因此更改列数和行数以及颜色的批处理文件应在 Windows 命令处理器完成批处理文件执行之前将它们恢复到初始值。

请参阅 DosTips 论坛主题 ECHO。无法给出文本或空行 - 而是使用 ECHO/ 来解释为什么

echo.
echo(
替换。

请参阅我的回答 为什么在命令行上使用 'set var = text' 后没有带有 'echo %var%' 的字符串输出? 它解释了在批处理文件中使用

set "variable=value"
的原因。

请参阅我的答案如何阻止 Windows 命令解释器在错误的用户输入上退出批处理文件执行? 对于所有额外的行,确保用户输入字符串不包含导致退出批处理文件处理的字符通过使用用户输入字符串的其他命令。

数字比较是通过使用字符串比较来完成的,这种比较速度要快一些,因为批处理文件中的环境变量和数字始终是字符串类型而不是整数类型。有关字符串比较与整数比较的更多详细信息,请参阅我对 Windows 批处理文件中与 NEQ、LSS、GTR 等等效的符号的回答。

强烈建议根据用户输入字符串在

echo %TALK%
和重定向运算符
|
之间添加空格。该空格也由命令 ECHO 输出,这就是为什么前四个 FINDSTR 执行的搜索字符串也以空格字符结尾,以便在用户真正输入四个问题之一时获得积极匹配。

A FINDSTR 搜索字符串(如

"how are you"
)会导致在行中搜索单词
how
OR 单词
are
OR 单词
you
。需要使用
/C:"how are you"
才能真正搜索短语
how are you
。另请参阅:为什么 FINDSTR 在我的批处理文件中找不到搜索到的带有空格的字符串?

输出使用环境变量

Output
连接,但前四个问题直接通过单个回复处理。不启用延迟扩展是为了更方便输出带有感叹号的字符串。

所有

^
都被删除,因为 Windows 命令处理器将其解释为转义字符。更容易避免该字符,而不是增加批处理脚本代码的复杂性,以便在连接字符串多次输出到更长的字符串时正确处理该字符。

对所使用的 Windows 命令使用完整限定文件名作为 Windows 系统目录中的可执行文件,可以提高效率,因为

cmd.exe
无需每次都搜索这些可执行文件。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

  • call /?
  • color /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?
  • title /?

另请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.