Loops 循环中的Findstr,批处理文件

Loops 循环中的Findstr,批处理文件,loops,batch-file,findstr,Loops,Batch File,Findstr,我想请你帮忙。 我有一个文本文档colors.txt,其中包含许多颜色(数百种颜色,每种颜色在单独的行中) 例如: blue white yellow green magenta cyan white black 我有多个文件夹,其中包含子文件夹和文件。 我必须制作一个脚本(批处理文件),逐行搜索所有文件夹、子文件夹和文件中的颜色。如果特定颜色至少使用一次,则一切正常。但是,如果这些文件夹、子文件夹和文件中完全没有使用(找不到)某种颜色,我必须知道它是哪种颜色 可以手动执行此操作,并使用以下命

我想请你帮忙。 我有一个文本文档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
如果在目录
C:\Temp\NotFoundColors.txt及其子目录中的*.txt文件中至少找到一次所有颜色,则输出文件
C:\Temp\Test
为空。如果输出文件为空,则上面的批处理文件将删除该文件

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

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • del/?
  • echo/?
  • endlocal/?
  • findstr/?
  • 获取/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?
另见:

  • 关于Microsoft的文章

    • 非常感谢您的回答

      我对命令提示符和批处理文件没有太多经验,所以在多次尝试之后,我决定用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());
              }
          }
      }
      
      使用系统;
      使用System.IO;
      使用System.Linq;
      类所有对象
      {
      静态void Main()
      {
      字符串basedObj_FP=@“基于对象的文件路径”;
      字符串[]basedObj_FL=File.ReadAllLines(basedObj_FP);/*基于对象的文件行*/
      int baseObj_FL_length=basedObj_FL.length;/*基本对象文件行长度*/
      字符串workingFile_FP=@“工作文件的文件路径”;
      字符串textToFind_FP=“0”/*要查找的文本的文件路径*/
      字符串finalFile_FP;/*最终文件的文件路径*/
      对于(int baseObj_FL_member=0;baseObj_FL_memberl.Contains(textToFind));/*查找所需内容*/
      if(desccontent!=null)
      {
      textToFind_FP=obj;/*将描述内容中的路径或所需对象分配给textToFind_FP*/
      finalFile_FP=@“确定文件路径”;
      文件处理(最终文件FP、文本查找、文本查找FP);
      }
      }
      如果(texttofindu FP==“0”)
      {
      finalFile_FP=@“未使用的文件路径”;
      文件处理(最终文件FP、文本查找、文本查找FP);
      }
      textToFind_FP=“0”;
      }
      }
      /*
      *函数创建并写入最终文件
      */
      静态无效文件\u处理(字符串fFP、字符串tTF、字符串tTF\u FP)
      {
      尝试
      {
      如果(!File.Exists(fFP))/*如果文件不存在*/
      {
      使用(FileStream fs=File.Create(fFP))/*创建它*/
      {
      ;
      }
      }
      如果(File.Exists(fFP))/*如果文件存在*/
      {
      使用(var tw=新StreamWriter(fFP,true))
      {
      如果(tTF_FP!=“0”)
      {
      tw.WriteLine(“{0,-40}{1}”,tTF,tTF_FP);
      }
      如果(tTF_FP==“0”)
      {
      tw.WriteLine(“{0,-40}未找到”,tTF);
      }
      }
      }
      使用(StreamReader sr=File.OpenText(fFP))/*读取所写内容*/
      {
      字符串s=“”;
      而((s=sr.ReadLine())!=null)
      {
      控制台。写入线(s);
      }
      }
      }
      捕获(例外情况除外)
      {
      Console.WriteLine(例如ToString());
      }
      }
      }
      
      打开命令提示窗口,键入
      findstr/?
      并按enter键。然后,您将看到已经使用的命令的用法信息,并且应该注意从speci获取搜索字符串的
      /G
      选项
      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());
              }
          }
      }