2

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?

Fortranner
  • 2,525
  • 2
  • 22
  • 25

2 Answers2

5

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))

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

I needed to search for filenames which contained one specific string ("up"), but did not contain another string ("packages").

However, I was hoping to run it from the command line.

This is actually possible to do, you just have to call findstr twice.

Mine looked like: dir /B /S up | findstr /I "up" | findstr /I /v "packages"

That means:

  1. search all directories (/S & subdirs)
  2. give me the bare formatting (/B)
  3. and pass it through (| pipe) findstr (/I ignore case) to find ones that have "up" then
  4. pass the results (| pipe) through findstr again but this time ignore all that contain (/v) "packages"

If you have items like:

  • c:\test\packages\up
  • c:\extra\thing\up
  • c:\extra\thing\packages\up
  • c:\extra\test\up
  • c:\extra\test\nothing

The results would be only the ones that contain "up" but do not contain "packages"

  • c:\extra\thing\up
  • c:\extra\test\up

Call findstr /v multiple times on result

In other words you can keep passing the result into another findstr with /v to remove the ones that have additional words you don't want.

raddevus
  • 8,142
  • 7
  • 66
  • 87