Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Moving all nested folders up one level

I have an annoying problem trying to move all nested folders up one level.

This is the basic folder structure:

20200101\Yada\Folder01
Folder02
Folder03
20200102\Yada\Folder01
Stuff01
ThisThing
20200103\Yada\Folder01
Folder03
ThatThing
TheOtherThing

Etc. The problem is "Yada" (actually a code like: 4E0DA4CE99D71EB5F8B7FF2DCD2AC6AF). That's a folder name placed there by a misconfigured file sync program. The various folders, Folder01, Folder02, Folder03, etc, should all be under their respective dated folders. I just need to get rid of "Yada". There are about six years worth of these folders so I obviously don't want to do it manually. There are only two variations of "Yada", as far as I can see, so a command file to do this would only need to be run twice, and I could then just recursively delete all the Yadas (making sure they are empty first).

I've been trying various forms of a FOR DO loop using xcopy and I can't get anything to work.
 
Something like this maybe? MOVE with /N instead of /Q will show you what would be done (without doing it).

Code:
v:\20200101> tree /f

V:\20200101
└──yada
   ├  file1
   └──folder01
      └  file2

v:\20200101> move /s /q v:\20200101\yada v:\20200101\

v:\20200101> tree /f

V:\20200101
├  file1
└──folder01
   └  file2
 
I guess I just needed some sleep. I used to use ren to move folders all the time back in the day (the MS-DOS days, which were amazing). I was having trouble with the for loop this time. I came up with two lines. The first renames the folders, thus emptying out Yada. The second deletes all empty folders in the tree, thus removing Yada. (And any other empty folders in the tree, but that doesn't matter in this case since any empty folders also would be an error in the folder tree and need to be removed anyway).

It's not the cleanest solution. It needs to be run from the root folder of the tree you want to process. But it works.

Now I want to tailor it to other uses. Like when you get one of those irritating 7z files where somebody archived the root folder. This could move everything up a level if tailored to accept a folder name as an argument.

Code:

for /d %%a in ("%_cwds*") do for /d %%b in ("%%a\Yada\*") ren "%%b" %@replace[Yada\,,"%%b"]
for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do robocopy %d %d /s /move
 
Back
Top