在循环、批处理文件中查找str

问题描述 投票:0回答:2

我想请你帮忙。 我有一个文本文档colors.txt,其中包含许多颜色(数百种颜色,每种颜色都在单独的行上)。

例如:

blue
white
yellow
green
magenta
cyan
white
black

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

可以手动执行此操作并使用以下命令测试所有颜色:

findstr /s/m "blue" *.txt

但是真的有数百个,而且需要很长时间。

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

loops batch-file findstr
2个回答
0
投票

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

@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 命令提示符窗口执行:

(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\NotFoundColors.txt
及其子目录的 *.txt 文件中至少找到一次,则输出文件
C:\Temp\Test
为空。上面的批处理文件如果为空则删除输出文件。

带有颜色的文本文件不得存储在

findstr
搜索到的目录中,或者必须具有不同的文件扩展名。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

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

另请参阅:


0
投票

非常感谢您的回答,

我对命令提示符和批处理文件没有太多经验,所以经过多次尝试,我决定用 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());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.