19

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

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

becomes

|parent
|    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.

4 Answers 4

29

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.

2
  • +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. Commented Mar 24, 2017 at 15:09
  • Amazing solution - my only issue is the naming of the folders. I have a LOT of folders to do this to and they can't be mixed - are there variables that represent current path and current path minus one?
    – Hellreaver
    Commented Nov 25, 2019 at 9:27
6

forfiles /s /m *.FILETYPE /c "cmd /c move @file .." should move any file of specified FILETYPE into its parent folder

3
  • That's the right answer, some other solutions here are not able to copy in upper folder (this has to be relative path to current path of file) because FOR takes absolute file path and '..' is upper folder of FOR command not of file
    – sbrbot
    Commented Nov 8, 2020 at 1:21
  • 2
    But this moves everything, including the folders up ONE level from where it ran. You might as well use Explorer to select everything in the current folder, and drag mouse it all up one folder
    – Fandango68
    Commented Jan 13, 2021 at 1:35
  • 1
    If the task is to move all files within a folder regardless of location, one can search for all files of type and move them using explorer yes. OP is asking each file (not folders) matching the filetype to be moved up one level (it's parent folder). If you have several layers of folders and multiple folders/files in each one, you cannot copy paste using explorer (well not in one go, and without manual work). I appreciate the comment though.
    – Serkan
    Commented Feb 25, 2021 at 22:44
5

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.

2

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.

1
  • 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
    Commented Nov 13, 2015 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .