planck
planck

Reputation: 45

How to shorten a directory path pattern

I have a set of about 10.000 directories that have the following structure

./AAA/AAA/somedirs/file.txt
..
./BCDE/BCDE/somedirs/file.txt
..
./FGMAB/FGMAB/somedirs/file.txt
..
etc

I want to reduce the recurring directory path pattern of two consecutive directories with the same name:

./X/X/etc/

to a single directory with that name:

./X/etc/

I thought of finding all substructures after such a X/X pattern and move them to X/ with a command like:

for /r /d %x in (*/%y/%y/*) do move "%x" ".."

This does run, but it doesn't seem to actually do anything.

Anyone an idea if and how this could be done?

Upvotes: 2

Views: 3762

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36308

Assuming that the duplicate names are all at the top of the tree structure as shown, something like this should work:

md dummy\x
for /D %x in (*) do if exist %x\%x cmd /c "move %x dummy\x\%x & move dummy\x\%x\%x %x & rd dummy\x\%x"

Test it first! (And remember the percentage signs need to be doubled up when the command is in a batch file.)

Upvotes: 1

Related Questions