cmdfindstr

Use Windows findstr to list files not containing a string


From Windows CMD I can use

findstr -m subroutine *.f90

to list all files with suffix .f90 containing "subroutine". To list all .f90 files not containing the string I can do something like

dir /b *.f90 > files.txt
findstr -m subroutine *.f90 > files_with_string.txt

and then write a script to list the lines in files.txt not found in files_with_string.txt. Is there a more elegant way?


Solution

  • There is a /v option in findstr, but that wouldn't help here.
    Process each file with a for loop, try to find the string and if it doesn't find it (||), echo the filename:

    for %a in (*.f90) do @findstr "subroutine" "%a" >nul || echo %a
    

    (above is command line syntax. For use in a batchfile, use %%a instead of %a (all three occurences))