This Windows batch script is to move thousands of files and splitting them up so that there are set number of files per folder. This example script splits 200 files per folder but you can customize it to your need. Also it checks file extensions and moves only certain file types, eg. jpg, jpeg, png, webp, mp4 etc. File type selection is also customizable to your need. Example code:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Config parameters
SET groupsize=200
:: initial counter, everytime counter is 1, we create new folder
SET n=1
:: folder counter
SET nf=0
FOR %%f IN (*.jpg) DO (
:: if counter is 1, create new folder
IF !n!==1 (
SET /A nf+=1
MD folder!nf!
)
:: move file into folder
MOVE /Y "%%f" folder!nf!
:: reset counter if larger than group size
IF !n!==!groupsize! (
SET n=1
) ELSE (
SET /A n+=1
)
)
ENDLOCAL
PAUSE
To set number of files to move per folder, set the value in: SET groupsize=200
To set the file type to move, set the value in: FOR %%f IN (*.jpg) DO (
Original question ” Moving a large number of files in one directory to multiple directories” was asked on Stackoverflow 11 years ago. The best answer was given by aphoria, based on which the batch script is created.
It is basically moving a large number of files from a single folder to dynamically created multiple new folders with X numbers of files per folder. This is NOT a solution for other type of use cases like “Batch script to move files from one folder to another based on date” or “Windows batch file to move files to subfolders based on file name”.
Sample windows batch script for moving 200 PNG files to new folders in batch:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Config parameters
SET groupsize=200
:: initial counter, everytime counter is 1, we create new folder
SET n=1
:: folder counter
SET nf=0
FOR %%f IN (*.png) DO (
:: if counter is 1, create new folder
IF !n!==1 (
SET /A nf+=1
MD folder!nf!
)
:: move file into folder
MOVE /Y "%%f" folder!nf!
:: reset counter if larger than group size
IF !n!==!groupsize! (
SET n=1
) ELSE (
SET /A n+=1
)
)
ENDLOCAL
PAUSE