Page 1 of 1

Processing a group of files at a time in a single folder

Posted: 03 Jan 2012 23:36
by xga
Hi Guys,

I'm wanting to process approximately 20 random files in a single folder so that they are rar'ed (compressed using the 3rd party rar.exe command) in groups of 5 files, making for a total of 4 rar files (rar1.rar, rar2.rar, rar3.rar and rar4.rar), containing all of the 20 files. Ideally I want to be able to rar the first 5 files in the folder, perform a move of that rar file and then move onto the next 5 files to be rar'ed, and then repeat until there are no more files left.

I'm a bit stumped at the moment on where to start. I'm able to rar multiple files at a time, I'm just not sure how to rar the first 5 files and then move onto the next 5 and so on, until finished. I'm guessing some sort of FOR /F loop is required?

I haven't been able to find any example code either. Can anyone point me in the right direction? Let me know if I need to clarify anything.

Re: Processing a group of files at a time in a single folder

Posted: 03 Jan 2012 23:56
by Aacini
I don't know how to rar 5 files at a time (and you don't show it), so I suppose it is achieved this way:

Code: Select all

rar options.. file1 file2 file3 file4 file5 /output=name
If so, then the Batch file below solve your problem:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set i=0
set group=0
set "fiveFiles="
for %%f in (*.*) do (
   set "fiveFiles=!fiveFiles! %%f"
   set /A i+=1, mod=i %% 5
   if !mod! == 0 (
      group+=1
      rar options.. !fiveFiles! /output=rar!group!.rar
      set fiveFiles=
   )
)

Re: Processing a group of files at a time in a single folder

Posted: 04 Jan 2012 02:54
by xga
Hi Aacini,

Thank you very much for your prompt reply.

I had a problem with the group variable incrementing, so I changed the line "group+=1" to "set /a group+=1" and now the script is working perfectly.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set i=0
set group=0
set "fiveFiles="
for %%f in (*.*) do (
   set "fiveFiles=!fiveFiles! %%f"
   set /A i+=1, mod=i %% 5
   if !mod! == 0 (
      set /a group+=1
      rar a rar!group!.rar !fiveFiles!
      set fiveFiles=
   )
)


One thing I did notice though, if I have say only 19 files in the folder, only the first 15 files will be rar'ed into 3 rar files and the last 4 files are ignored. I'm struggling to work out the logic to catch any files that might be missed if I don't have exactly 20 files in the folder. Any suggestions?

Thanks once again!

Re: Processing a group of files at a time in a single folder

Posted: 04 Jan 2012 12:55
by Aacini
Ops! My fault on forget that SET /A...

To check if there are missed files after the FOR ends, just check if fiveFiles variable is not empty:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set i=0
set group=0
set "fiveFiles="
for %%f in (*.*) do (
   set "fiveFiles=!fiveFiles! %%f"
   set /A i+=1, mod=i %% 5
   if !mod! == 0 (
      set /A group+=1
      rar a rar!group!.rar !fiveFiles!
      set fiveFiles=
   )
)
if not "%fiveFiles%" == "" set /A group+=1 & rar a rar!group!.rar !fiveFiles!