Windows 7 Command Line – Move All Files from Subfolders to Parent Folder

cmd.execommand linewindowswindows 7windows-explorer

Firstly, I'm aware there are tons of similar questions, and I know because I must have tried adapting at least 5 of them, but I'm still here asking because my crappy grasp of CMD (which I actually thought I was getting good at up until this point) is frustrating me with generic, unhelpful errors, and I can't understand what part of the syntax is wrong. This is likely due to the fact that other questions deal with the folder/filename structures of their respective OPs, and when I try to adapt the code to my own folder/file structure it refuses to work. Therefore, I'm here asking for a CMD for loop solution that'll actually work for my own file and folder structure.

I have a parent folder which contains many other folders, in which there are files:

Parent Folder
     Subfolder
           File1
           File2
     Subfolder
           File1
           File2
     Subfolder
           File1
           File2

…and so on.

I'm wanting to use a CMD for loop in combination with the move command to extract all the files in the subfolders to the parent folder, leaving each subfolder empty.

When giving the CMD for loop please break down what each part of the command is doing, as this'll help make sure I understand the code and hopefully minimise the trouble I have getting it to work for myself.

Best Answer

Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.

To Extract All Files from Subfolders to their Parent Folder

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

Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.

To Extract All Files from Subfolders and Delete Empty Subfolders

It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.

for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"

Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.

By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.

The Batch File

Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.

@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent% 
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent% 
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL
Related Question