简体   繁体   English

如何使批处理文件更智能

[英]How to make the batch file smarter

I'm trying to make a batch file which checks if the user input does exist in xy.txt well thats easy 我正在尝试制作一个批处理文件,检查用户输入是否确实存在于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\

but now if the user input is "hello world" i want to check each word individually. 但是现在,如果用户输入的是“ hello world”,我想分别检查每个单词。

i tried this but it isn't good... and it saves if the word does not exist 我尝试了这个,但它不是很好。。。如果这个词不存在,它可以保存

    @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\

Couldn't I do that in a more easier and smarter way? 我不能以更轻松,更智能的方式做到这一点吗?

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM