如何在下次使用时加载的批处理程序中保存某些内容?

时间:2013-04-26 21:47:25

标签: batch-file

我正在使用Batch来创建一种'AI'程序。这是非常基本的,我对此很好,但是如果我打开程序并告诉它保存某些东西(文本),无论如何都要这样做吗?当我下次启动程序时输入某个东西时,它会加载吗?

1 个答案:

答案 0 :(得分:1)

不确定这是否是您想要的......因为您没有指定是否需要“人工AI”或AI用于其他类型的bot-Task。

这只是我在几分钟内写的例子,但也许这可以帮助你开始:

@Echo off

:Ask
Echo+
SET /P "Question=>>[YOU] "
Call :Answer "%Question%"
GOTO:ASK

:Answer
Echo+
For /F "Usebackq Tokens=1,* Delims=;" %%A in ("AI.txt") Do (
    For %%@ in (%Question%) DO (
        Echo "%%A"| FINDSTR /I "\"%%@\"" 1>NUL && (
            Echo:   [AI] %%B
            GOTO:ASK
        )
    )
)
Echo:   [AI] I don't understand you.
GOTO:EOF

enter image description here

AI.txt内容:

matrix;Don't talk about it if you don't awake... 
pennys;You said pennys? That remembered me I'm so hungry... ;)  
dog;I like it!
name;My name is Bachitia, yes it's cool...
  

第2部分

这是另一种AI ......

@Echo off
Setlocal enabledelayedexpansion

Set /A "INDEX=1"

REM :Check_Question {INDEX} {Response if answer is not saved} {Response if answer is saved}

Call :Check_Question "_%INDEX%" "Hello, I'm glad to meet you, What's your name? " "Welcome again {Value}"
Call :Check_Question "_%INDEX%" "What are your hobbies? " "Are you {Value} again? "

Pause&Exit

:Check_Question
If not exist "AI.txt" (Echo _0;DUMMY>"AI.txt")
For /F "Usebackq Tokens=1,* Delims=;" %%A in ("AI.txt") Do (
    If "%%A" EQU "%~1" (
        Set "Answer=%~3"
        Set "Answer=!Answer:{Value}=%%B!"
        Echo !Answer!
        Set /A "INDEX+=1"
        Exit /B 0
    )
)
Set /P "Question=%~2"
Echo _%INDEX%;%Question%>>"AI.txt"
Set /A "INDEX+=1"
Exit /B 0

首次运行:

enter image description here

Ai.txt自动生成的内容:

_0;DUMMY
_1;Elektro
_2;programming

第二次运行:

enter image description here