Visual studio Find files that does NOT contain X

前端 未结 4 1848
一向
一向 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: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 filesList = new List();
    
        foreach (string line in lines)
        {
            if (!line.StartsWith("\t"))
            {
                filesList.Add(line.Substring(0, line.LastIndexOf('(')).Trim());
            }
        }
    
        List notContainedFiles = new List();
        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());
    }
    

提交回复
热议问题