1

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.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Jimmy
  • 57
  • 1
  • 5
  • It's probably because `dir` does not sort numbers "numerically", `10.jpg` for example sorts between `1.jpg` and `2.jpg` – DavidPostill Feb 21 '18 at 18:20
  • 2
    Possible duplicate of [Sorting a list in progressive numeric order](https://stackoverflow.com/questions/23544268/sorting-a-list-in-progressive-numeric-order) – DavidPostill Feb 21 '18 at 18:20
  • 1
    See [Sorting a list in progressive numeric order](//stackoverflow.com/q/23544268) for a workaround – DavidPostill Feb 21 '18 at 18:21
  • Thanks for the reply, the only problem with that solution is it says its for files without leading zeros. I should have put an example of the files I'm trying to sort. Heres an example. 02C_CN201S7P_00001.tif 02C_CN201S7P_00002.tif 02C_CN201S7P_00003.tif 02C_CN201S7P_00004.tif 02C_CN201S7P_00005.tif – Jimmy Feb 21 '18 at 19:27
  • Then I don't know what the issue is – DavidPostill Feb 21 '18 at 20:18

1 Answers1

0

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143