0

Sorry if the question is confusing. Basically, I have a log.txt with a list of directories. Is there a way to loop through the list, and see if any of the directories contain a folder with a specific name. So if the log has :

C:\test1

C:\test2

and we are looking for a folder called "test3" inside any of the directories in the log, then output that it was found.

edit: I am new to batch and kinda mashed other code i found on here to make this, apologies if its a mess.

for /F "tokens=*" %%Z in (%dir1%\log.txt) do (

    cd %%Z

    for /d /r "%%Z" %%a in (*) do if /i "%%~nxa"=="test3" set "foldername=%%a"

    echo "%foldername%"
)

where dir1 is the directory I'm using elsewhere in the code, and test3 is the string I'm looking for in the subfolder name.

I think I need to use SetLocal EnableDelayedExpansion? but I don't really understand it.

7
  • Show us the batch file code you've tried so far and where you are getting stuck. Commented Feb 19, 2020 at 19:12
  • 1
    sorry, I didnt really know where to start so i didnt have much code, I updated the post thank you Commented Feb 19, 2020 at 19:29
  • 1
    Your question states, "test3 is the string I'm looking for in the subfolder name.", but your code suggests, "test3 is the string I'm looking for as the subfolder name.". Which is true? Commented Feb 19, 2020 at 21:28
  • I am looking to see if a folder contained in each directory of the log file has the correctly named subfolder in it. So it should look inside C:\test1 for a folder with test3 in the name, then look in C:\test2 for a folder with test3 in the name. Thank you for your help. Commented Feb 20, 2020 at 2:43
  • 1
    You've continued to make your requirement unclear, is it supposed to be "with test3 in the name"? or "with the name test3"? For instance, is TheFastest3Miles supposed to match? or only test3? Commented Feb 20, 2020 at 15:44

1 Answer 1

0

The following syntax will return all lines within your text file that contain a folder name you specify:

type log.txt | findstr foldername

I tested this by creating a text file that contained the following:

test1 line 1
test2 line 2
test3 line 3
test2 line 4

And then I ran:

type mytextfile.txt | findstr test2

And it returned:

test2 line 2
test2 line 4

Is this basically what you were looking for?

Sign up to request clarification or add additional context in comments.

1 Comment

close. the text file should be directories, and then it should look in each directory to see if there is a folder called test3. so if the log file contains c:\test1 then c:\test2 then it should look in both for a folder called test3. apologies if my wording is confusing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.