[Batch] Search multiple folders for string in file

Solved
Ptitgrand Posted messages 13 Status Member -  
Ptitgrand Posted messages 13 Status Member -
Hello,

I would like to know if you could help me.

The goal of my program is to retrieve image files present in a folder whose name I don't know.
In this folder, there is a file that contains a string allowing me to find the right folder among several directories.

I am looking to design a small program that could search in different folders (with random names) for a "txt" file that contains a string (TOTO), and then I would like to retrieve the path that leads to the file.

Example:

Here are the different files:

C:\Test\ABC005\ex.txt (Contains line 25: abv)
C:\Test\ABC001\xe.txt (Contains line 25: abn)
C:\Test\ABC008\ab.txt (Contains line 25: TOTO)

I would like to run the program in batch to retrieve the path "C:\Test\ABC008" so that I can later copy the images to another directory.

Best regards,
DUBOIS Nicolas

2 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    You can try something like this:
    for /f "delims=" %%A in ('2^>nul dir /b /s C:\Test\*.txt') do (
    findstr /C:"TOTO" "%%~A" >nul && echo %%~dpA
    )
    pause
    exit


    “Artificial intelligence is defined as the opposite of natural stupidity.”
    1
    1. Ptitgrand Posted messages 13 Status Member
       
      Great!
      It worked, thank you :)
      0