I need a batch file script to move all files from a single parent folder into as many subfolders as necessary, as long as each subfolder receives the maximum amount of files specified in the batch file or at the command line. In other words, the script must distribute all files from the parent folder into multiple subfolders below it (which the script must create automatically), moving the files in lots of X files (where "X" is the amount of files each subfolder will receive).
Subfolder names must be created according to the following nomenclature: 001, 002, 003 and so on.
Must support files with all kinds of filenames (with spaces, with special characters or non-English accentuation, etc.).
Must support the moving of tens of thousands files.
Must work in Windows 10 Professional under local administrator rights.
For example, say the folder "D:\Downloads" has 2400 files in it, and that you want to distribute them in subfolders containing up to 1000 files each. After running the script, the following structure will be created:
D:\Downloads |__001 |__002 |__003
D:\Downloads --> Will have no files inside it anymore |__001 --> Will have 1000 files inside it |__002 --> Will have 1000 files inside it |__003 --> Will have the last 400 files inside it
The order of files (which file goes to which subfolder) is not important, that is, the move won't need to take any specific criteria into account (such as filename, size, filetype, etc.). However, any improvements on this aspect will be welcome (e.g., the option to move the last created files to the first subfolders first).
Here's the solution that worked for me:
@echo off setlocal enableextensions setlocal enabledelayedexpansion if not %3.==. goto syntax if %2.==. goto syntax :: Checks if %2 is a number: SET "var="&for /f "delims=0123456789" %%i in ("%2") do set var=%%i if defined var (goto syntax) if /i %1.==. goto syntax if /i not exist %1 echo. & echo The folder %1 does not exist... && echo Folder paths must be typed between "quotes" if there's any empty space. && echo. && goto end setlocal enableextensions setlocal enabledelayedexpansion :: Maximum amount of files per subfolder: SET limit=%2 :: Initial counter (everytime counter is 1, a new subfolder is created): SET n=1 :: Subfolder counter: SET nf=0 ::Retrieves the amount of files in the specified folder: set count=0 for %%A in (%1%\*.*) do set /a count+=1 echo. echo Distributing %count% files in subfolders of up to %2 files... FOR %%f IN (%1%\*.*) DO ( :: If counter is 1, create a new subfolder with name starting with "00...": IF !n!==1 ( SET /A nf+=1 MD %1%\00!nf! ) :: Move files into subfolders with names starting with "00...": MOVE /-Y "%%f" %1%\00!nf! > NUL :: Reset counter when a subfolder reaches the maximum file limit: IF !n!==!limit! ( SET n=1 ) ELSE ( SET /A n+=1 ) ) SET limit= SET n= SET nf= SET count= echo Move finished successfully. goto end :syntax echo. echo YOU TYPED: movedown %* echo SYNTAX: movedown ["full path"] (between quotes if there's any space) [n] (maximum number of files per subfolder) echo. :end ENDLOCAL