Batch file to process files in sub sub directory

192 Views Asked by At

Given the folder structure

Folder0
    batfile.bat
    Folder1
        Folder1a
            file1a1
            file1a2
        Folder1b
            file1b
        Folder1c
            file1c1
            file1c2

How would I process the files file1a1, file1a2, etc.?

I'd like to try something like:

@echo off
for %%i in ('dir Folder0') do (for %%j in ('dir %%i') do <stuff to %%j>)

but A) it doesn't work and B) doesn't seem like the right approach anyway.

Update: I think I found something that works:

@echo off
cd %1
for /r %%i in (*.*) do echo %%i

where echo represents that thing I want to do to the file. However, I would like to be more explicitly traversing the directory structure so that I have a variable that represents the directory names. I ultimately want to put the processed files in a mirrored directory structure.

Update 2: That doesn't quite work because once I cd into the lower directory, batfile.bat is one directory up, and I can't call it. I tried the equivalent of "../batfile.bat" and ../batfile.bat, but no luck.

1

There are 1 best solutions below

0
On BEST ANSWER

You can iterate over the folders with for /d command, combined with pushd and popd commands.

Read help for and help pushd, and try this code in a bat file

for /d %%a in (*) do (
   echo %%a
   pushd %%a
   for /d %%b in (*) do (
     echo %%a/%%b
   )
   popd
)