批处理脚本以移动n个文件交换时间 [英] batch script to move n files exch time
问题描述
我目前有一个脚本可以将temp_dir中的所有文件移动到dir并处理文件.我想更改它以批量移动n个文件来处理它们.通过批处理脚本实现此目标的最佳方法是什么.
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.
推荐答案
我不太确定您需要什么.
I'm not quite sure what you need.
- 您是否打算以N批处理每个文件,直到没有剩余文件?
- 还是您打算只处理每个目录的前N个文件,而忽略其余所有文件?
您可以使用模运算来每N个循环暂停一次.模数计算除法后的余数.如果模数为0,则分子被分母平均除.它是这样的:
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
以此类推.
这是一个示例批处理脚本,它具有取模操作,可以暂停每个%filesPerChunk%
循环迭代.使用 .bat
扩展名保存并尝试.
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
)
)
方案2:每个目录仅处理前N个文件
这可以通过一个简单的计数器来完成,该计数器针对遇到的每个文件递增,并在遇到新目录时重置为0.
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
)
)
这篇关于批处理脚本以移动n个文件交换时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!