在文本文件中找到一个单词并在 windows 批次中将其分配给一个变量

Find a word in a text file and assign it to a variable in windows batch

我正在尝试以 60 秒的间隔在 txt 文件中搜索一个词(因为 txt 文件的内容随时间变化),如果找到这个词,搜索将停止并执行一些代码。 我使用下面的代码找到了这个词,但我不知道如何将它分配给一个变量。

>nul find "SUCCESS" C:\usr\new.txt && (
  echo OK
) || (
  echo FAILED
)

谢谢。

这里要实现您的目标,您真正需要的只是一个保持循环、超时和 findstr,使用成功的结果来打破循环。

    @Echo Off
rem // define and qualify search file path
    Set "Searchee=%~1"
    If not exist "%Searchee%" (Echo/File not located & Exit /B)
rem // holding loop
:Hold
    (Type "%Searchee%" | (Findstr /L "SUCCESS")) >Nul 2> Nul && Goto :Act
    Timeout 60 >Nul
Goto :Hold
:Act
    Echo/String Located
rem // insert commands to be enacted