Windows 如何在xy.txt中单独查找输入(“hello world”)

Windows 如何在xy.txt中单独查找输入(“hello world”),windows,batch-file,command,Windows,Batch File,Command,我正在尝试创建一个批处理文件,用于检查用户输入是否存在于xy.txt中。这很简单 但是现在如果用户输入的是“hello world”,我想分别检查每个单词 我试过了 @setlocal enableextensions enabledelayedexpansion @echo off :start set /p word=" " for /F "tokens=* delims= " %%A in ("%word%") do set A=%%A & set B=%%B if %A

我正在尝试创建一个批处理文件,用于检查用户输入是否存在于xy.txt中。这很简单

但是现在如果用户输入的是“hello world”,我想分别检查每个单词

我试过了

@setlocal enableextensions enabledelayedexpansion
@echo off

:start
set /p word=" "

for /F "tokens=* delims= " %%A in ("%word%") do set A=%%A & set B=%%B 


if %A%=="" goto Anovalue
if not %A%=="" goto checkforA

:Anovalue
echo first word has no value
pause

 if %B%=="" goto Bnovalue
 if not %A%=="" goto checkforB

 :Bnovalue
 echo second word has no value
 pause
 goto start

 :checkforA
 findstr /c:"%A%" xy.txt > NUL
 if ERRORLEVEL 1 goto notexistA
 if ERRORLEVEL 2 goto existA

  :checkforB
  findstr /c:"%B%" xy.txt > NUL
  if ERRORLEVEL 1 goto notexistB
  if ERRORLEVEL 2 goto existB

  :existA
  echo first word does exist in xy.txt
  pause
  goto checkforB

  :existB
  echo second word does exist in xy.txt
  pause
  goto start

  :notexistA
  echo first word does not exist in xy.txt
  pause
  (echo %A%) >>xy.txt
  goto checkforB

 :notexistB
 echo second word does not exist in xy.txt
 pause
(echo %B%) >>xy.txt
goto start\

我能不能用一种更简单更聪明的方式来做

有很多方法可以完成您要求的任务,其中许多方法使用的代码要少得多。例如,给定以下文件
xy.txt

this is a test of the
system to see if it
will work the way
that i want it to
work today
此批处理文件(
check.bat
):

我们将返回以下内容:

c:\>check "is test smart"
    Found - is
    Found - test
Not Found - smart
然而,单词中的单词也会返回true。例如,
选中“day”
将找到
day
,即使它不是一个单独的单词,因为它是今天
的一部分。处理这种情况会有点棘手。为此,您需要用一些字符封装搜索词,然后用相同的封装字符替换
xy.txt
中的所有空格。例如,如果我们使用
,将
xy.txt
wh中的所有空格替换为
,然后搜索
.word.
,我们将只找到匹配的整个单词

@echo off

setlocal ENABLEDELAYEDEXPANSION

set words=%1
set words=!words:"=!
set words=.!words: =. .!.

for /f "tokens=* delims=" %%i in (xy.txt) do (
  set line=%%i
  set line=.!line: =.!.
  echo !line!>>xy.txt.tmp
)

for %%i in (!words!) do (
  set word=%%i
  set word=!word:.=!
  findstr /I /C:"%%i" xy.txt.tmp > NUL && echo     Found - !word! || echo Not Found - !word!
)

del xy.txt.tmp

endlocal
我选择创建一个中间文件
xy.txt.tmp
,以存放编辑过的文件,其中空格被替换为
。然后我们可以执行以下命令并获得显示的结果:

c:\>check "this is a test of the stem today that will work each day"
    Found - this
    Found - is
    Found - a
    Found - test
    Found - of
    Found - the
Not Found - stem
    Found - today
    Found - that
    Found - will
    Found - work
Not Found - each
Not Found - day

它可以正确地在行首、行尾以及两者之间的任何位置查找单词。唯一的缺点是它创建然后删除的中间文件。在没有中间文件的情况下执行此操作会有点复杂…

我今天已经回答了相同的问题哦,对不起,没有看到它。非常感谢,但这不是我想要的答案。还有,你想要的答案是什么?我想逐一检查每个单词的意思:首先是你好,然后是世界。我真的很感激你的回答。好的,谢谢你。提供的代码完全符合您的要求。对于输入的每个单词,测试它是否在文件中。很遗憾,在我花时间写这个答案之前,我没有在问题的第一条注释中单击@MCND提供的链接。我们的答案非常相似。当他们没有得到他们想要的答案时,OP肯定已经转载了这个问题。。。不管你怎么问这个问题,你都会得到一个非常相似的答案。是的,我很抱歉,但非常感谢。你能不能让程序在另一个单词里也会说false?我会更快乐,因为那是我真正想要的。我真的很感谢你的回答。我编辑了答案以显示如何排除部分单词匹配。谢谢,我试过了,但它对我不起作用。我只需按enter键,什么也没发生,我也不知道程序如何检查xy.txt中的单词,因为代码中只有一个!文件没有xy.txt,你能帮我吗?我真的很感谢你的回答。它总是
xy.txt
,或者有时可能是不同的文件名?您会注意到,在运行
check.bat
时,我将
xy.txt
作为参数之一。它是参数
%2
。因此,您可以像我一样,在命令行上传入
xy.txt
,或者我可以修改代码,以便对
xy.txt
进行硬编码。你喜欢什么?
c:\>check "this is a test of the stem today that will work each day"
    Found - this
    Found - is
    Found - a
    Found - test
    Found - of
    Found - the
Not Found - stem
    Found - today
    Found - that
    Found - will
    Found - work
Not Found - each
Not Found - day