5

I am trying to loop through a bunch of files in sub-directories to run a command on them, but I can't seem to figure out how to reach them.

The directory structure is like this:

  • Main directory (Holds .bat file and top-level directories)
    • Sub-directory
      • Search directory
        • A bunch of files (I need the locations of all of these separately)

I am able to get a list of all of the files/folders in the "Main directory" using:

for /f %%f in ('dir /b /r *') do echo %%f

but I cannot seem to figure out how to go any further into the directories to get the files in the "Search" directory.

An example of the file structure is:

C:\Users\swiftsly\Search160\0002\search\AP584.txt

Any help is greatly appreciated!

swiftsly
  • 811
  • 4
  • 16
  • 29
  • 3
    If you happened to read the HELP for the DIR command you may have noticed the **/S** option. But I would use a FOR /R command instead of FOR /F. – Squashman Dec 11 '15 at 19:57
  • @Squashman, `for /R` is fine as long as the body of the loop does not manipulate the directory tree; since the OP did not say what command is to be executed on the enumerated files, I would go for the `for /F`/`dir /B /S` option to be saver; see this post: [At which point does `for` or `for /R` enumerate the directory (tree)?](http://stackoverflow.com/q/31975093/5047996)... – aschipfl Dec 12 '15 at 00:37

1 Answers1

4

The final solution to this question, with the help of the recommendation by Squashman to use /R instead of /f is:

@echo off    
for /R %%f in (*.txt) do echo %%f

This will print out a list of all of the .txt files in the sub-directories.

swiftsly
  • 811
  • 4
  • 16
  • 29