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



和标题一样。如何更改以下批处理代码,使bot答案在一行中,而不是在几行中。当我写一个问题时,机器人在几行内回答我,当我输入对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

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

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BotName=Amanda"
set "Name=Ikki"
set "Lines="
set "Columns="
for /F "tokens=1,2 delims=: " %%I in ('%SystemRoot%System32mode.com') do if "%%I" == "Lines" (set "Lines=%%J") else if "%%I" == "Columns" set "Columns=%%J"
%SystemRoot%System32mode.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%System32findstr.exe /I /L /X /C:"how are you " >nul && (echo %BotName%: Good, you?& goto Begin)
echo %TALK% | %SystemRoot%System32findstr.exe /I /L /X /C:"how old are you " >nul && (echo %BotName%: 18& goto Begin)
echo %TALK% | %SystemRoot%System32findstr.exe /I /L /X /C:"what book you like " >nul && (echo %BotName%: I like LOTR book.& goto Begin)
echo %TALK% | %SystemRoot%System32findstr.exe /I /L /x /C:"what movie you like " >nul && (echo %BotName%: I like terminator movie.& goto Begin)
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<hello>" >nul && call :Hello
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<hi>" >nul && call :Hi
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<day>" >nul && call :Day
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<why>" >nul && call :Why
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<dogs>" >nul && call :Dogs
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<kill>" >nul && call :Kill
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<bot>" >nul && call :Bot
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<where>" >nul && call :Where
echo %TALK% | %SystemRoot%System32findstr.exe /I /R "<test>" >nul && set Output=%Output% A.I. Rulez! Ex Machina is the best movie ;)"
echo %TALK% | %SystemRoot%System32findstr.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%System32mode.com CON COLS=%Columns% LINES=%Lines%
endlocal

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

见DosTips论坛主题ECHO。不能给出文本或空行-使用ECHO/来解释为什么echo.echo(取代。

请参阅我的回答,为什么在命令行上使用'set var = text'后没有'echo %var%'的字符串输出?说明在批处理文件中使用set "variable=value"的原因。

看到我的回答如何阻止Windows命令解释器退出批处理文件执行错误的用户输入?对于所有额外的行,确保用户输入字符串不包含导致其他命令处理用户输入字符串时退出批处理文件的字符。

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

强烈建议根据用户输入字符串使用echo %TALK%和重定向操作符|之间的空格。该空间也可以通过命令ECHO输出这就是为什么前四个FINDSTR执行的搜索字符串也以空格字符结尾,以便在用户真正输入四个问题之一时获得正匹配。

"how are you"这样的搜索字符串会导致在行中搜索单词howare单词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 /?

参见:

  • Microsoft关于使用命令重定向操作符的文档
  • Windows命令解释器(CMD.EXE)如何解析脚本?
  • 使用Windows批处理文件的单行多命令
  • GOTO:EOF返回到哪里?

最新更新