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

Extra requirements:

  1. Subfolder names must be created according to the following nomenclature: 001, 002, 003 and so on.

  2. Must support files with all kinds of filenames (with spaces, with special characters or non-English accentuation, etc.).

  3. Must support the moving of tens of thousands files.

  4. 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 --> 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).

Any ideas?


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

echo  YOU TYPED: movedown %*
echo  SYNTAX: movedown ["full path"] (between quotes if there's any space) [n] (maximum number of files per subfolder)
  • No, it isn't magic site that doing a free job. Wrong place. Here you may ask some particular question when you have done something but stuck on some problem. As of now, your question looks more like a job assignment for freelancers instead of the question – Alex Aug 21 '18 at 20:36
  • I tried it many times, in different ways, without success. I didn't describe all my attempts in my question not to make it too big and because they were all on the wrong track anyway... – Rodrigo Faustini Aug 21 '18 at 20:47
  • Show your attempts, don't hesitate, otherwise people who would give you solution will be downvoted which isn't fair. As an idea: make two variables, one that will hold name of directory to move in (say dir=001) and another one that count files, increment it after each move command, when it equal to 1000, reset it to 1, increment another variable (dir) that holds directory name and do it until all files moved. – Alex Aug 21 '18 at 21:09
  • Thank you, Alex! Your tip was the base for the solution. – Rodrigo Faustini Aug 29 '18 at 13:01
  • You did it ! I voted to remove hold from your question. If you would have in a future some questions, made them as a question, show what you tried even if there is some problem, it easier to understand a problem for those who willing to help. You can post your own solution as an answer and accept it as resolved, it is completely legal way to contribute to knowledge database that helps everybody. – Alex Aug 29 '18 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.