I'm pretty new to to working with batch files. I need to split a folder of about 500k images into smaller folders of about 5000 each. I have been able to do this but they do not split sequentially. Image 1 will end up in one folder while image 2 is in a completely different one for example. Here's what I have so far.
@echo off
setlocal enabledelayedexpansion
set source=C:\Desktop\test
set numfiles=0
set numdirs=1
set filelimit=5000
for /F "tokens=*" %%G in ('dir "%source%" /A:-D /B') do (
set /A numfiles+=1
set target=0000!numdirs!
set target=!target:~-5!
if not exist "%source%\!target!" md "%source%\!target!"
move "%source%\%%G" "%source%\!target!"
if [!numfiles!]==[%filelimit%] (
set /A numdirs+=1
set numfiles=0
)
)
Example of file names:
02C_CN201S7P_00001.tif
02C_CN201S7P_00002.tif
02C_CN201S7P_00003.tif
Any help would be much appreciated.
On FAT16, FAT32 and exFAT drives the file names are not sorted by name as on NTFS drives. For that reason it is recommended to use the DIR option /ON
to get the list of file names ordered by name.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "source=C:\Desktop\test"
set "numfiles=0"
set "numdirs=1"
set "filelimit=5000"
for /F "delims=" %%G in ('dir "%source%" /A:-D /B /ON 2^>nul') do (
set /A numfiles+=1
set "target=0000!numdirs!"
set "target=!target:~-5!"
if not exist "%source%\!target!" md "%source%\!target!"
move "%source%\%%G" "%source%\!target!"
if !numfiles! == %filelimit% (
set /A numdirs+=1
set "numfiles=0"
)
)
Adding /ON
on DIR command line is the only modification made on batch file code with an effect on execution in comparison to code posted in question.
The square brackets around the two environment variable references comparing the two numbers as strings have no special meaning and are not needed here as both environment variables are always defined. For that reason the four square brackets are removed from last IF condition command line.