A batch script can efficiently organize thousands of video files into separate folders by grouping them in sets of forty. By iterating through a directory, the script calculates the target folder name based on the current file count and moves each file accordingly. This approach ensures that each drama series or collection is split into manageable chunks without manual intervention.
Understanding the Grouping Logic
The core challenge involves tracking how many files have been processed to determine when to create a new folder. You need a counter that increments with every file moved. When this counter reaches the limit (40), you must increment a "group" variable and reset the counter for the next batch. This logic prevents all files from ending up in a single folder or creating an infinite loop of empty directories. The script relies on standard Windows command-line variables to maintain state across iterations within the same execution context.
The Batch Script Solution
Below is a complete, robust batch script that implements this logic. It assumes your video files are in a specific source directory and defines a destination root where the grouped folders will be created.
@echo off
setlocal enabledelayedexpansion
:: Configuration
set "SourceDir=C:\Videos\Raw"
set "DestRoot=C:\Videos\Organized"
set "GroupSize=40"
:: Initialize counters
set /a FileCount=0
set /a GroupNum=1
:: Ensure destination root exists
if not exist "%DestRoot%" mkdir "%DestRoot%"
:: Loop through all video files
for %%F in ("%SourceDir%\*.mp4" "%SourceDir%\*.mkv" "%SourceDir%\*.avi") do (
set /a FileCount+=1
:: Calculate which group folder this file belongs to
set /a GroupIndex=(FileCount - 1) / GroupSize + 1
set "GroupFolder=%DestRoot%\Drama_Group_!GroupIndex!"
:: Create folder if it does not exist
if not exist "!GroupFolder!" mkdir "!GroupFolder!"
:: Move the file
move "%%F" "!GroupFolder\"
echo Moved "%%~nxF" to "!GroupFolder!"
)
echo Done. Total files processed: %FileCount%
pause
This script uses
enabledelayedexpansion to correctly handle variable updates inside the loop. Without this setting, the variables would resolve only once at the start of the loop, leading to incorrect folder assignments. The calculation
(FileCount - 1) / GroupSize + 1 ensures that files 1-40 go to Group 1, 41-80 to Group 2, and so on.
Adjusting File Types
The
for loop explicitly lists common video extensions like
.mp4,
.mkv, and
.avi. If you have other formats such as
.wmv or
.mov, simply add them to the list within the parentheses. Be careful not to include generic patterns like
*.* unless you want to process every single file in the directory, including thumbnails or metadata files.
Common Pitfalls and Fixes
Many users encounter issues because they forget to enable delayed expansion. If you see errors about variables being undefined, ensure
setlocal enabledelayedexpansion is at the top. Another common mistake is using
%Variable% instead of
!Variable! inside the loop body. Standard percent signs expand immediately before the loop starts, while exclamation marks expand during execution, reflecting the current iteration's value.
If you find that files are not moving, check your permissions. Running the script as an administrator may be necessary if the source directory is protected. Additionally, verify that the path lengths do not exceed Windows' maximum limits, though modern versions of Windows 10 and 11 handle long paths better than previous versions. For more complex scheduling needs, you might consider integrating this script into Task Scheduler, as discussed in our guide on
Powershell Script Not Running in Task Scheduler.
Verifying the Output
After running the script, navigate to your destination root folder. You should see subdirectories named
Drama_Group_1,
Drama_Group_2, etc. Each folder should contain exactly 40 files, except possibly the last one, which will contain the remainder of the total file count. Check a few folders to ensure the files were moved correctly and that no duplicates remain in the source directory.
For troubleshooting variable persistence issues if you decide to refactor this into multiple scripts, refer to
Persisting Windows Variables in CMD Files. This knowledge helps if you need to save the last processed group number for future incremental runs.
Frequently Asked Questions
What happens if I have fewer than 40 files?
The script will create a single group folder containing all available files. The math ensures that even one file triggers the creation of Group 1, so you won't end up with empty folders unless you have zero files to process.
Can I change the folder naming convention?
Yes, simply modify the line defining
GroupFolder. You can use dates, episode ranges, or custom prefixes instead of
Drama_Group_. Just ensure the string remains consistent and free of invalid filename characters.
Does this script work on network drives?
It works on network drives provided you have read/write permissions and the connection is stable. However, large batches over slow networks may time out. Consider processing smaller subsets if you experience connectivity drops.
How do I handle files with spaces in their names?
The script already accounts for spaces by quoting the paths around variables like
"%%F" and
"!GroupFolder!". This ensures that filenames with spaces are treated as single entities rather than broken arguments.
Can I run this script repeatedly without duplicating files?
No, because
move removes the file from the source. If you re-run it on the same source directory, it will find no files left. To avoid accidental data loss, always back up your source directory before running bulk move operations.