Finding files that do NOT contain a string using Windows "findstr" command

Posted: (EET/GMT+2)

 

Today, I was working on a set of files generated by an application. Suddenly, I needed to find files that specifically did not contain a specific string. Here's a simple solution that doesn't require any extra tools, just Windows itself.

So, if you need to list files under a directory tree that do not contain a specific string, you can do it directly in Windows command prompt ("cmd.exe") with a for /R loop and findstr.

The idea is simple: recurse through files, test each one with findstr, and only print the filename when the search fails. The findstr command uses the exit code to indicate failure (no match found), which in turn can be utilized using the "double-pipe" || "run if fails" statement. Here's an example:

for /R %f in (*.txt) do @findstr /C:"searchtext" "%f" >nul || echo %f

Here's how it works:

  • for /R walks the current directory and all subdirectories.
  • %f is the current file path.
  • @findstr /C:"searchtext" searches for the exact string inside the file. The ampersand @ stops Windows from echoing the command itself.
  • >nul hides normal output.
  • || echo %f runs only if findstr returns no match.

Tip: if you run this inside a batch file, double the percent sign:

@echo off
for /R %%f in (*.txt) do findstr /C:"searchtext" "%%f" >nul || echo %%f

This pattern is handy when you want a quick negative content filter without switching to PowerShell.