循环中的Findstr,批处理文件

Findstr in loop, batch file

我想请你帮忙。 我有一个包含多种颜色的文本文档 colors.txt(数百种颜色,每种颜色各占一行)。

例如:

blue
white
yellow
green
magenta
cyan
white
black

我有文件夹,其中包含子文件夹和文件。 我必须制作一个脚本(批处理文件),逐行搜索所有这些文件夹、子文件夹和文件的颜色。如果至少使用一次特定颜色,则一切正常。但是,如果在这些文件夹、子文件夹和文件中的任何一个中某些颜色完全未使用(找不到),我必须知道它是哪种颜色。

可以手动完成并使用以下命令测试所有颜色:

findstr /s/m "blue" *.txt

但是真的有几百个,时间太长了。

是否有可能通过循环来完成,参数根据 colors.txt 中的行而变化?

此批处理文件可用于此任务:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ColorsFolder=C:\Temp"
set "SearchFolder=C:\Temp\Test"
set "OutputFile=%ColorsFolder%\NotFoundColors.txt"

rem Search for each color in colors.txt in all text files of the folder tree
rem and output those colors not found in any text file into the output file.
(for /F "usebackq delims=" %%I in ("%ColorsFolder%\colors.txt") do %SystemRoot%\System32\findstr.exe /I /M /R /S /C:"\<%%~I\>" "%SearchFolder%\*.txt" >nul || echo %%I)>"%OutputFile%"

rem Delete output file on being empty if all colors were found.
for %%I in ("%OutputFile%") do if %%~zI == 0 del "%OutputFile%"
endlocal

唯一需要的命令行是 for 命令行,它也可以从 Windows command prompt window 执行:

(for /F "usebackq delims=" %I in ("C:\Temp\colors.txt") do %SystemRoot%\System32\findstr.exe /I /M /R /S /C:"\<%~I\>" "C:\Temp\Test\*.txt" >nul || echo %I)>"C:\Temp\NotFoundColors.txt"

如果在目录 C:\Temp\Test 及其子目录的 *.txt 文件中至少找到一次所有颜色,则输出文件 C:\Temp\NotFoundColors.txt 为空。上面的批处理文件如果为空则删除输出文件。

带有颜色的文本文件不得存储在 findstr 搜索的目录中,或者必须具有不同的文件扩展名。

要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅:

非常感谢您的回答,

我对命令提示符和批处理文件没有太多经验,所以经过多次尝试,我决定用 C# 编写一个小程序。它更复杂,但似乎工作正常。

using System;
using System.IO;
using System.Linq;

class All_Obj
{
    static void Main()
    {
        string basedObj_FP = @"File path of a based object";
        string[] basedObj_FL = File.ReadAllLines(basedObj_FP);   /* File lines of a based object */                                          
        int baseObj_FL_length = basedObj_FL.Length;              /* Base object file lines length */                                      
        string workingFile_FP = @"File path of working file";                
        string textToFind_FP = "0";                              /* File path of text to find */                                       
        string finalFile_FP;                                     /* File path of final file */                                       

        for (int baseObj_FL_member = 0; baseObj_FL_member < baseObj_FL_length; baseObj_FL_member++)     /* Base object file lines member */
        {
            string textToFind = basedObj_FL[baseObj_FL_member];                                         /* Text to find */

            string[] allObj = Directory.GetFiles(workingFile_FP, "*.txt", SearchOption.AllDirectories); /* All the objects in working file including subdirectories */

            foreach (string obj in allObj)
            {
                string[] lines = File.ReadAllLines(obj);                                                /* Read all lines of object */
                string desContent = lines.FirstOrDefault(l => l.Contains(textToFind));                  /* Find desired content */
                if (desContent != null)
                {
                    textToFind_FP = obj;                                                                /* Assign path in desContent or desired object to textToFind_FP */
                    finalFile_FP = @"OK File path";
                    file_Handling(finalFile_FP, textToFind, textToFind_FP);
                }
            }
            if (textToFind_FP == "0")
            {
                finalFile_FP = @"Unused File path";
                file_Handling(finalFile_FP, textToFind, textToFind_FP);
            }
            textToFind_FP = "0";
        }


    }
    /*
     * function creating and writing to final files
     */

    static void file_Handling(string fFP, string tTF, string tTF_FP)
    {
        try
        {
            if (!File.Exists(fFP))                       /* If File does not exist */                                                   
            {
                using (FileStream fs = File.Create(fFP)) /* Create it */                                     
                {
                    ;
                }
            }

            if (File.Exists(fFP))                         /* If File exists */                                                 
            {
                using (var tw = new StreamWriter(fFP, true))
                {
                    if (tTF_FP != "0")
                    {
                        tw.WriteLine("{0,-40} {1}", tTF, tTF_FP);
                    }
                    if (tTF_FP == "0")
                    {
                        tw.WriteLine("{0,-40} not found", tTF);
                    }
                }
            }

            using (StreamReader sr = File.OpenText(fFP))   /* Read what is written */                                       
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }

        catch (Exception ex)
        {
        Console.WriteLine(ex.ToString());
        }
    }
}