Search code examples
batch-fileerrorlevel

Batch file: Search for text and proceed on error


I've tried to write a batchfile, which would check it's own directory for other batch files and check these for a certain text. If the text is found, I want the program to jump to the end, otherwise copy itself into the found file. Here's how I tried:

    rem windowsisajoke
    for %%f in (*.bat) do (set A=%%f)
    set FILE=%A%
    set CONTENT=windowsisajoke
    findstr /i "%CONTENT%" %FILE% >NUL
    if errorlevel 0 goto end
    copy %0 %A%
    :end

Solution

  • if errorlevel 0 actually means "if errorlevel is zero or greater". (I know - very intuitive...)

    Either change your logic:

    if errorlevel 1 copy %0 %A%
    

    or use

    if %errorlevel%==0 goto :end