如何使此for循环仅对每100个文件中的1个执行?

时间:2018-10-21 11:40:57

标签: batch-file

我需要这个for循环才能在每100个文件中运行1个...

exp(zz[i] + pf[i]*log(zk[i]) + nf[i]*log(zy[i]) + sum{j in J}m*log(ap[j])
zk[i] - klb[i] >=0
zy[i] - ylb[i] >=0
kub[i] - zk[i]>=0
yub[i] - zy[i] >=0
where i varies from 1 to 20, j varies within each i.

问题是为所有文件分配了相同的编号,因此跳过了整个文件集,所有文件被处理的机率约为1:100。

2 个答案:

答案 0 :(得分:2)

@echo off
setlocal enabledelayedexpansion
for %%a in (*) do (
  set /a rnd=!random! %% 100
  rem echo !rnd!
  if !rnd! == 0 ECHO process "%%~fa" 
)
生成

rnd来保存0到99之间的数字。因此if触发的概率为每个文件1%(使用if !rnd! lss 25 ...的概率为25%)。
这平均为您的文件 的1%。

答案 1 :(得分:1)

只是一些想法。

@echo off
    setlocal enableextensions disabledelayedexpansion

    echo(
    echo Case 1, one file each 100
    echo(

    rem Initialize counter variable. 
    rem Here two options, fixed start or random start
    set "n=0"
    set /a "n= %random% %% 100"

    rem While hidding stderr
        rem For each file
            rem Increase the counter and calculate 1 / n mod 100
            rem Calc will fail if n is a multiple of 100
            rem Use conditional execution operator to detect failure and 
            rem echo the file name

    2>nul (
        for /f "delims=" %%a in ('dir /s /b /a-d') do (
            set /a "n+=1", "1/(n %% 100)" || echo %%a
        )
    )

    echo(
    echo Case 2, random file selection with a 1/100 probability
    echo( 

    setlocal enabledelayedexpansion
    2>nul (
        for /f "delims=" %%a in ('dir /s /b /a-d') do (
            set /a  "1/(!random! * 100 / 32768)" || (
                setlocal disabledelayedexpansion
                echo %%a
                endlocal
            )
        )
    )
    endlocal 

    echo(
    echo Case 3, random 1/100 file selection 
    echo(

    rem Generate a list of files with a random prefix, sort the list and then 
    rem retrieve one file each 100 with the same method in case 1

    set "n=0"
    2>nul (
        for /f "tokens=1,*" %%a in ('
            dir /s /b /a-d 
            ^| cmd /q /e /v /c"for /f delims^= %%a in ('find /v ""') do set /a !random! & echo  %%a"
            ^| sort
        ') do (
            set /a "n+=1", "1/(n %% 100)" || echo %%b
        )
    )