How to conditionally take action if FINDSTR fails to find a string

后端 未结 4 1004
时光取名叫无心
时光取名叫无心 2020-12-08 07:21

I have a batch file as follows;

CD C:\\MyFolder
findstr /c:\"stringToCheck\" fileToCheck.bat
IF NOT XCOPY \"C:\\OtherFolder\\fileToCheck.bat\" \"C:\\MyFolder         


        
4条回答
  •  误落风尘
    2020-12-08 08:05

    I tried to get this working using FINDSTR, but for some reason my "debugging" command always output an error level of 0:

    ECHO %ERRORLEVEL%
    

    My workaround is to use Grep from Cygwin, which outputs the right errorlevel (it will give an errorlevel greater than 0) if a string is not found:

    dir c:\*.tib >out 2>>&1
    grep "1 File(s)" out
    IF %ERRORLEVEL% NEQ 0 "Run other commands" ELSE "Run Errorlevel 0 commands"
    

    Cygwin's grep will also output errorlevel 2 if the file is not found. Here's the hash from my version:

    C:\temp\temp>grep --version grep (GNU grep) 2.4.2

    C:\cygwin64\bin>md5sum grep.exe c0a50e9c731955628ab66235d10cea23 *grep.exe

    C:\cygwin64\bin>sha1sum grep.exe ff43a335bbec71cfe99ce8d5cb4e7c1ecdb3db5c *grep.exe

提交回复
热议问题