I would normally open the parent folder and search for * in order to select all of the files in subfolders, but in this instance, I have over 1,000,000 files that I need to sort through, so my explorer often crashes when trying to copy that many files through the GUI. I am not sure how much more effective this will be through the command prompt or a batch file, but it is worth a try, I suppose.

What I need to do is make it so that

|    |123
|    |    123abc.png
|    |456
|    |    456def.png
|    |789
|    |    789ghi.png


|    123abc.png
|    456def.png
|    789ghi.png

Yes, my actual file structure has the first 3 characters of the file name given to the folder name, if that can help at all in sorting these.


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.

  • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders. – bigp Mar 24 '17 at 15:09

This is some sample code:

    for /d %%D in (%1\*) do (move "%%D\*" %1\ && rmdir "%%D")
    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.

  • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help. – DavidPostill Nov 13 '15 at 13:03

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.