【问题标题】:how to find input ("hello world") individually in xy.txt如何在 xy.txt 中单独查找输入(“hello world”)
【发布时间】:2013-11-08 11:08:37
【问题描述】:

我正在尝试制作一个批处理文件来检查 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\

难道我不能以更简单、更聪明的方式做到这一点吗?

【问题讨论】:

  • 我今天回答了同样的问题here
  • 哦抱歉没看到。非常感谢,但这不是我要找的答案..
  • 那么,您在寻找什么答案?
  • 我想逐个检查每个单词的含义:首先是你好,然后是世界。非常感谢您的回答。
  • 好的,谢谢。提供的代码完全符合您的要求。对于输入中的每个单词,测试它是否在文件中。

标签: windows batch-file command


【解决方案1】:

有很多方法可以完成您要求做的事情,其中​​许多方法使用的代码要少得多。例如,给定以下文件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):

@echo off
setlocal ENABLEDELAYEDEXPANSION

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

endlocal

返回以下内容:

c:\>check "is test smart"
    Found - is
    Found - test
Not Found - smart

但是,单词中的单词也会返回 true。例如,check "day" 将找到 day,即使它不是一个单独的词,因为它是 today 的一部分。处理这种情况会有点棘手。为此,您需要用某个字符封装搜索词,然后将xy.txt 中的所有空格替换为相同的封装字符。例如,如果我们使用.,将xy.txt 中的所有空格替换为.,然后搜索.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 没有得到他们正在寻找的答案时,他们一定是重新发布了这个问题......似乎无论你如何提出问题,你都会得到一个非常相似的答案。
  • 是的,很抱歉,但非常感谢。你能不能让它即使这个词是另一个词,程序也会说假?我会更开心,因为那是我真正在寻找的。非常感谢您的回答。
  • 我编辑了答案以显示如何排除部分单词匹配。
  • 谢谢,我试过了,但对我不起作用。我只是按回车键,没有任何反应,我也不知道程序如何检查 xy.txt 中的单词,因为在代码中只有一个!文件!并且没有 xy.txt 你能帮帮我吗?非常感谢您的回答。
  • 会一直是xy.txt,还是有时是不同的文件名?你会注意到我在运行check.bat 时将xy.txt 作为参数之一。它是参数%2。所以你可以像我一样在命令行中传入xy.txt,或者我可以修改他的代码,以便xy.txt是硬编码的。你的偏好是什么?
猜你喜欢
  • 2015-05-24
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多