Move all files from multiple subfolders into the parent folder

Use FOR /R at the command prompt:

[FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.

First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.

In your case the command would look something like this:

FOR /R "C:\Source Folder" %i IN (*.png) DO MOVE "%i" "C:\Staging Folder"

If you want to put this into a batch file, change %i to %%i.

Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.

Once the move is complete, you can rename/move the staging folder as required.

TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.


This is some sample code:

:loop
    for /d %%D in (%1\*) do (move "%%D\*" %1\ && rmdir "%%D")
    SHIFT
    set PARAMS=%1
if not %PARAMS%!==! goto loop

With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.

'Shift' is to move to the next argument, when you drag many subfolder at once on the script.


Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.

SetLocal EnableDelayedExpansion   
FOR /R "C:\Source Folder" %%i IN (*.png) DO (       
       Set "CurrFile=%%~i"     
       Set "TMPdp=%%~dpi"   
       Set "ParFldr=!TMPdp:~0,-1!"    
       @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.

The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.