-2

I would like to ask you for a help. I have a text document colors.txt that contains many colors (hundreds of them, each on the separate line).

For example:

blue
white
yellow
green
magenta
cyan
white
black

Than I have folders, that contain subfolders and files. I have to make a script (batch file), that searches for colors thru all those folders, subfolders and files line by line. If specific color is used at least once, everything is OK. However, if some colour is completely unused (can not be found) in any of these folders, subfolders and files, I have to know which color it is.

It is possible to do it manually and test all the colors with command such this one:

findstr /s/m "blue" *.txt

But there are really hundreds of them and it would take too long.

Is there any possibility to do it through loop, with argument that varies according to lines in colors.txt?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Marek Majkút
  • 13
  • 1
  • 3
  • 1
    Open a Command Prompt window, type `findstr /?` and press the enter key. You will then see the usage information for the command you have already used, and should note the `/G` option which gets search strings from a specified file, in your case `colors.txt`. – Compo Sep 17 '19 at 16:27

2 Answers2

0

This batch file can be used for this task:

@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

The only needed command line is the for command line which could be executed also from a Windows command prompt window with:

(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"

The output file C:\Temp\NotFoundColors.txt is empty if all colors were found at least once in a *.txt file in directory C:\Temp\Test and its subdirectories. The batch file above deletes the output file if being empty.

The text file with the colors must not be stored in directory searched by findstr or must have a different file extension.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

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

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

Thank you very much for your answer,

I do not have much experience with command prompt and batch files, so after many tries I decided to make a small program in C#. It is more complex but it seems to work fine.

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());
        }
    }
}
Marek Majkút
  • 13
  • 1
  • 3