0

I know about for loops such as

for /r %f in (*) do @move %f .

and

for /r %f in (.) do @move %f .

but I can't seem to find any code that fits my scenario. Say I have many directories that look like this:

Folder 1 
  Folder 2
    File 1
    File 2
    File 3
    Folder 3
      File 4
Folder 4 
  Folder 5
    File 5
    File 6
    File 7
    Folder 6
      File 8

and I want to get File 4 into Folder 2, File 8 into Folder 5, and so on, how would I go about this? For this scenario, I have quite a few directories that look like this, so manually going into every single one would not work. What's the simplest solution, preferably using Batch? The end result would hopefully look like this:

Folder 1
  Folder 2
    File 1
    File 2
    File 3
    File 4
Folder 4
  Folder 5
    File 5
    File 6
    File 7
    File 8
0
1

This set of commands should put all files in a directory into the top-level directory - so all files under Folder 1 will be put directly into Folder 1, and all files under Folder 4 will be put directly into Folder 4.

You will need to modify it to get the exact setup that you describe in the question, but this should hopefully be a good start. Also, I'm relatively new to batch scripting, so test this against some unimportant, temporary directories and files before attempting to do anything on the real data.

dir /ad /b *your current directory* > dirs.txt
for /F "eol=" %i in (dirs.txt) do @echo dir /a-d /b /s %i ^> %i.txt >> script.bat
.\script.bat
for /F "eol=" %j in (dirs.txt) do for /F "eol=" %i in (%j.txt) do @move %i %j

Credit to this question for explaining how to recursively list only files and this question for explaining how to use a for loop with a text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.