Visual Studio 查找不包含 X 的文件

发布于 2024-11-05 04:15:13 字数 135 浏览 3 评论 0原文

我需要一种方法来列出所有包含已知文本的文件。

该项目包含 1000 多个文件,我只想要不包含某些文本的文件。

我想到了正则表达式,但它没有这样的功能。

有人知道解决办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟雨凡馨 2024-11-12 04:15:13

从命令提示符处:

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

例如:

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 文档 此方法可以帮助您找到最适合您的文件搜索词。

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.

风向决定发型 2024-11-12 04:15:13

更新:正如 Peter McVoy 指出的那样,这个解决方案不起作用。它将返回误报(包含一行但没有要搜索的文本的文件也将被打印)。 VS 宏或自定义 powershell 脚本可能是最佳选择。我稍后可能会重新审视这个答案。

作为参考,这是我的答案:

您可以在命令行上执行此操作:

findstr /S /V /M text_to_search_for *.cs

这将打印所有不包含文本的文件名

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

空城仅有旧梦在 2024-11-12 04:15:13

您可以使用在文件中查找功能notepad++

步骤:

  1. 输入您要查找的单词并选择目录。
  2. 复制搜索结果。
  3. 对其进行过滤以检索包含该单词的文件列表。
  4. 然后在 C# 中运行一个简单的循环来获取不在此列表中的文件列表。这些是不包含该单词的文件。

这是获取文件列表的循环(在 .cs 文件中搜索)(可以优化):

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());
}

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());
}
偏爱自由 2024-11-12 04:15:13

从命令窗口(“DOS 框”)使用 findfindstr

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文