I currently have a script to move all files in a temp_dir to dir and process files. I would like to change it to move n files in batches to process them. What would be best way to achieve it through batch script.
Asked
Active
Viewed 1,871 times
1 Answers
0
I'm not quite sure what you need.
- Do you intend to process every file in batches of N until no files remain?
- Or do you intend to process only the first N files per directory, ignoring all the rest?
Scenario 1: processing every file in batches of N
You could use a modulo operation to pause every N loops. A modulus calculates the remainder after a division. If the modulus is 0, then the numerator is divided evenly by the denominator. It works like this:
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
and so on.
Here's an example batch script with a modulo operation to pause every %filesPerChunk%
loop iterations. Save this with a .bat
extension and try it out.
@echo off
setlocal enabledelayedexpansion
set /a "filesPerChunk=5, idx=0"
for /F "delims=" %%I in ('dir /s /b') do (
echo Processing %%I
set /a "idx+=1"
set /a "mod=idx %% filesPerChunk"
if !mod! equ 0 (
echo --- END OF CHUNK ---
pause
)
)
Scenario 2: processing only the first N files for every directory
This can be done with a simple counter that increments for each file encountered and resets to 0 when a new directory is encountered.
@echo off
setlocal enabledelayedexpansion
set filesPerChunk=5
for /F "delims=" %%I in ('dir /s /b') do (
if "!dir!"=="%%~dpI" (
set /a "idx+=1"
) else (
if defined dir echo ---
set idx=0
set "dir=%%~dpI"
)
if !idx! lss %filesPerChunk% (
echo Processing %%I
)
)

rojo
- 24,000
- 5
- 55
- 101
-
/L may not work as we need to /R to copy all sub-directories. The requirement is to copy/move 5/n files from each sub directory. – Raj Apr 24 '13 at 20:56
-
@Raj - I'm not sure what you're asking. I updated my answer with a couple of example scripts. Does either do what you need? – rojo Apr 25 '13 at 16:54
-
I will try this. The situation is I have a directory with videos which I would copy to temp and process. There will be times when I have 20-30 files that would take a long time to complete for loop on dir. Instead I would like to copy a batch (n number) of them, process and copy them off to another server. I do not want to move all files using robocopy but for loop to move first 'n' number of files from that dir to process. – Raj Oct 23 '13 at 23:25