jerone
jerone

Reputation: 16861

Visual studio Find files that does NOT contain X

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

The project contains over 1000 files and I only want the ones that does not contain some text.

I thought of Regular expressions, but it doesn't have such a feature.

Anyone knows a solution?

Upvotes: 15

Views: 8771

Answers (4)

shannon
shannon

Reputation: 8774

From a command prompt:

@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

Microsoft's documentation on this method can help finding the best file search term for you.

Upvotes: 22

jeroenh
jeroenh

Reputation: 26772

UPDATE: as Peter McVoy pointed out, this solution does not work. It will return false positives (files that contain a line without the text to search for will also be printed). A VS macro or a custom powershell script is probably the way to go. I might revisit 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 filenames that do not contain the text

Upvotes: 4

Devendra D. Chavan
Devendra D. Chavan

Reputation: 8991

You can use the Find in Files feature of notepad++.

Steps:

  1. Type in the word you want to find and select the directory.
  2. Copy the search result.
  3. Filter it to retrieve the file list containing the word.
  4. And then run a simple loop in C# to get the list of files that are not in this list. These are the files that do not contain the word.

Here is the loop to get the files list is (searches 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());
}

Upvotes: 3

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Use find or findstr from a command window ('DOS box').

Upvotes: 1

Related Questions