如何使批处理文件更智能

时间:2013-11-08 06:06:03

标签: file batch-file command

我试图创建一个批处理文件来检查xy.txt中是否存在用户输入 那很容易

    @echo off 
    set /p input=" "

    findstr /c:"%word%" xy.txt > NUL
    if ERRORLEVEL 1 goto notexist
    if ERRORLEVEL 2 goto exist

    :exist
    echo user input does exist 
    pause > nul
    exit

    :notexist
    echo user input does not exist 
    pause > nul\

但现在如果用户输入是" 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\

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

1 个答案:

答案 0 :(得分:0)

@echo off

    setlocal enableextensions enabledelayedexpansion

    set words=
    set /p "words=What to search ? :"
    if not "%words%"=="" for %%w in ("%words: =" "%") do (
        findstr /c:%%w xy.txt > nul && set "_status=found" || set "_status=not found"
        echo %%w !_status!
    )

    endlocal