Visual studio Find files that DO NOT contain X

I need a way to list all files that do NOT contain known text.

The project contains more than 1000 files, and I only need those that do not contain any text.

I thought about regular expressions, but it does not have such a function.

Does anyone know a solution?

+8
regex find visual-studio-2008 visual-studio
source share
4 answers

At the command line:

@for /r %f in (FILE_SPECIFIER) do @find "YOUR_STRING" "%f" > nul || echo %f 

For example:

 C:\web-trunk\Views>@for /r %f in (*.cshtml) do @find "Layout" "%f" > nul || echo %f C:\data\web-trunk\Views\Account\RegisterPartial.cshtml C:\data\web-trunk\Views\Admin\SiteInspector.cshtml C:\data\web-trunk\Views\CandidateProfile\View.cshtml C:\data\web-trunk\Views\Common\ContactFooterForInfoEmails.cshtml C:\data\web-trunk\Views\Common\ContactFooterForInfoPages.cshtml C:\data\web-trunk\Views\Common\ContactFooterForSalesEmails.cshtml C:\data\web-trunk\Views\Common\ContactFooterForSalesPages.cshtml 
+14
source share

You can use the Find in files function notepad ++ .

Steps:

  • Enter the word you want to find and select a directory.
  • Copy search result.
  • Filter it to get a list of files containing the word.
  • And then run a simple loop in C # to get a list of files that are not on this list. These are files that do not contain words.

Here is a loop to get a list of files (search in .cs files) (can be optimized):

 private void GetFileNamesNotContainingWord(string filePath, string searchDirectoryPath, string notContainingFilePath) { string[] lines = File.ReadAllLines(filePath); List<string> filesList = new List<string>(); foreach (string line in lines) { if (!line.StartsWith("\t")) { filesList.Add(line.Substring(0, line.LastIndexOf('(')).Trim()); } } List<string> notContainedFiles = new List<string>(); foreach (FileInfo file in new DirectoryInfo(searchDirectoryPath).GetFiles("*.cs", SearchOption.AllDirectories)) { if (!filesList.Contains(file.FullName)) { notContainedFiles.Add(file.FullName); } } File.WriteAllLines(notContainingFilePath, notContainedFiles.ToArray()); } 
+3
source share

UPDATE: as Peter McVoy noted, this solution does not work. It will return false positives (files containing a string without search text will also be printed). A VS macro or a custom powershell script is probably the way to go. I can revise this answer later.

For reference, this was my answer:

You can do this on the command line:

 findstr /S /V /M text_to_search_for *.cs 

This will print all file names that do not contain text.

+3
source share

Use find or findstr from the command window ("DOS box").

+2
source share

All Articles