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.
Processing a group of files at a time in a single folder
Moderator: DosItHelp
Re: Processing a group of files at a time in a single folder
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: If so, then the Batch file below solve your problem:
Code: Select all
rar options.. file1 file2 file3 file4 file5 /output=name
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
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.
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!
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
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:
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!