【发布时间】: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