[Resolved] Moving incrementally named files to three different directories, evenly

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
koko
Posts: 10
Joined: 13 Oct 2016 00:40

[Resolved] Moving incrementally named files to three different directories, evenly

#1 Post by koko » 30 Jun 2019 06:59

Hi,

Been a while since I last posted. Sorry to come asking for help like this but after something unfortunate that occurred (family related) it's found me needing a way to move a directory of sequentially named images (that vary in total number depending on the export to that directory) from a directory into three sub-directories, in three groups (roughly one third each into each sub-directory).

I looked around for a way to achieve this, and came as far as enumerating the number of files in the source directory and dividing it by three but I end up without the complete number of files per group that way for some totals due to the apparent rounding of the 1/3 value.

I also found this post which describes performing a loop of commands based on a fixed number of per file group iterations.

Is there a way to reconcile the concept, so that for example when the directory has 394 total image files the first group moved would be the first 131 files, the second group the next 131 images, then the last 132? Basically accounting for non-whole values in the groupings by appending the remaining file to the last group moved when splitting by a third?

Haven't gotten to the move commands yet but currently encountered this issue when testing the calculation. Perhaps the solution obvious but I'd appreciate any insight with this. Thanks.
Last edited by koko on 30 Jun 2019 08:19, edited 2 times in total.

aGerman
Expert
Posts: 3670
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Moving sequentially named files to three different directories, evenly

#2 Post by aGerman » 30 Jun 2019 07:22

Not sure what "sequentially named" and "image files" means in particular because you didn't provide any example.
If you are only looking for the math, then it's quite easy.

Code: Select all

for /f %%i in ('dir /a-d /b *.jpg^|find /c /v ""') do set /a "third=%%i/3, remaining=%%i-2*third"
echo %third% %third% %remaining%
If "image files" is anything else but *.jpg (e.g. *.png, *.gif, or whatever) then you have to update the DIR command accordingly.

Steffen

koko
Posts: 10
Joined: 13 Oct 2016 00:40

Re: Moving sequentially named files to three different directories, evenly

#3 Post by koko » 30 Jun 2019 08:19

aGerman wrote:
30 Jun 2019 07:22
Not sure what "sequentially named" and "image files" means in particular because you didn't provide any example.
If you are only looking for the math, then it's quite easy.

Code: Select all

for /f %%i in ('dir /a-d /b *.jpg^|find /c /v ""') do set /a "third=%%i/3, remaining=%%i-2*third"
echo %third% %third% %remaining%
If "image files" is anything else but *.jpg (e.g. *.png, *.gif, or whatever) then you have to update the DIR command accordingly.

Steffen
Thank you very much. By 'sequential' I now realize I should have written 'incremental' to be more accurate/clear. I updated the topic title.

In my case the files are named '0000.jpg' to '0394.jpg' (for example). The code for enumerating the directory is such when I wrote the OP (the structure of the for loop sourced from a post elsewhere):

Code: Select all

@echo off
setlocal enabledelayedexpansion

set "source=<pathtodirectoryhere>"

for /f %%A in ('dir "!source!" ^| find "File(s)"') do set totalfiles=%%A
echo File count = !totalfiles!
Following your post I then this solution for moving just the first n number of files into each directory (I copied the for loop three times in the code each with a different destination directory for each loop).

Appears to run perfectly. Thanks again for the direction!

Post Reply