Visual studio Find files that does NOT contain X

前端 未结 4 1846
一向
一向 2021-02-13 14:42

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 s

相关标签:
4条回答
  • 2021-02-13 15:25

    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
    
    0 讨论(0)
  • 2021-02-13 15:32

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

    0 讨论(0)
  • 2021-02-13 15:44

    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

    0 讨论(0)
  • 2021-02-13 15:47

    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());
    }
    
    0 讨论(0)
提交回复
热议问题