如何使批处理文件更聪明 [英] How to make the batch file smarter

查看:122
本文介绍了如何使批处理文件更聪明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果让用户输入并在xy.txt存在哪些检查一个批处理文件
这就是好易

 关闭@echo
    集/ P输入=    FINDSTR / C:%字%xy.txt> NUL
    如果ERRORLEVEL 1页转到办法抹杀
    如果ERRORLEVEL页转到存在    :存在
    回声用户输入确实存在
    暂停> NUL
    出口    :不存在
    回声用户输入不存在
    暂停> NUL \\

但现在,如果用户输入的hello world我要逐个检查每个字。

我试过,但它是不好的......如果这个词是不存在这样可以节省

  @setlocal ENABLEEXTENSIONS enabledelayedexpansion
    关闭@echo    :开始
    集/ p字=    FOR / F令牌= * delims =%%的(%字%)一个不设A = %% A和集B = %%乙
    如果%A%==转到Anovalue
    如果没有%A%==转到checkforA    :Anovalue
    回声第一个字没有价值
    暂停    如果%B%==转到Bnovalue
    如果没有%A%==转到checkforB   :Bnovalue
   回波第二个单词没有价值
   暂停
   转到启动   :checkforA
   FINDSTR / C:%A%xy.txt> NUL
   如果ERRORLEVEL 1页转到notexistA
   如果ERRORLEVEL页转到existA   :checkforB
   FINDSTR / C:%B%xy.txt> NUL
   如果ERRORLEVEL 1页转到notexistB
   如果ERRORLEVEL页转到existB  :existA
  呼应第一个单词xy.txt确实存在
  暂停
  转到checkforB  :existB
  呼应第二个字在xy.txt确实存在
  暂停
  转到启动  :notexistA
  回声第一个字不存在xy.txt
  暂停
  (回声%A%)GT;> xy.txt
  转到checkforB  :notexistB
  回波第二个单词中不存在xy.txt
  暂停
  (回声%B%)GT;> xy.txt
   转到启动\\

我不能这样做更容易和更聪明的方式?


解决方案

 关闭@echo    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion    集字=
    集/ P字,即要搜寻?
    如果不是%字%==为%% W的(%的话:=%)做(
        FINDSTR / C:%%W¯¯xy.txt> NUL&功放;&安培;设置_status =发现||设置_status =找不到
        回声%%W¯¯!_status!
    )    ENDLOCAL

I'm trying to make a batch file which checks if the user input does exist in xy.txt well thats easy

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

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

这篇关于如何使批处理文件更聪明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆