batch script to move n files exch time

喜你入骨 提交于 2021-02-11 16:52:56

问题


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.


回答1:


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
    )
)


来源:https://stackoverflow.com/questions/14917997/batch-script-to-move-n-files-exch-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!