• Home
  • SEM
    • PPC
    • SEO
    • How To
  • Bangladesh
  • School
    • HTTP
    • REP
    • Bots n’ htaccess
    • Source Code
    • Excel 2010
    • HTML XHTML Entities
    • Gmail Operators
    • HTML & ASCII
  • About
    • Privy
    • About This Blog
  • Contact
  • বাংলা

Saidul Hassan

Digital Marketing Evangelist

Home / How to / Script / Batch script to create directories & moving fixed number of files to those directories

Batch script to create directories & moving fixed number of files to those directories

27 Sep, 2021 By Saidul Hassan · Filed Under: Script

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

Sample windows batch script for moving 200 JPEG files to new folders in batch:

The following two tabs change content below.
  • Bio
  • Latest Posts
My Twitter profileMy Facebook profileMy LinkedIn profileMy Instagram profileMy Pinterest profile

Saidul Hassan

Managing Partner at Up Arrow Consulting
COO at Up Arrow Consulting, MCC manager, & Technical SEO consultant. Certified Google Partners and Microsoft Bing Ads Accredited Professional, Python enthusiast, wannabe SysAdmin. Graduated from School of Management & Business Administration (SOMBA), Khulna University.
My Twitter profileMy Facebook profileMy LinkedIn profileMy Instagram profileMy Pinterest profile

Latest posts by Saidul Hassan (see all)

  • Batch script to create directories & moving fixed number of files to those directories - 30 Apr, 2021
  • How to use PrismJS syntax highlighter on WordPress without plugin - 30 Mar, 2020
  • Download an Entire Website for Offline Viewing - 26 Nov, 2019

Share:

  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Pocket
  • Email
  • Print

First published on 30 Apr, 2021 · Last updated 27 Sep, 2021 · Tagged With: Batch File move, Batch Script, Move file to directory, Windows Batch File

Batch script to create directories & moving fixed number of files to those directories

30 Apr, 2021 By Saidul Hassan

How to use PrismJS syntax highlighter on WordPress without plugin

30 Mar, 2020 By Saidul Hassan

Download an Entire Website for Offline Viewing

26 Nov, 2019 By Saidul Hassan

How to color highlight .htaccess files in Xed/Gedit

20 Aug, 2018 By Saidul Hassan

HMA Pro VPN Setup for Multiple Locations without User/Password Every time in Linux CLI

14 May, 2018 By Saidul Hassan

  • ♥ Bangladesh ♥
    Log in · Privacy Policy · Contact
    Copyright © 2011 Saidul Hassan

  • DMCA