Move all files from multiple subfolders into the parent folder
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.
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.
forfiles /s /m *.FILETYPE /c "cmd /c move @file .."
should move any file of specified FILETYPE into its parent folder
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.