vbscript将文件分为四组

时间:2016-11-16 18:11:25

标签: vbscript

我正在开发一个脚本,将文件夹中的文件数分成四组。这些将被转换为四个批处理文件,但目前问题是将它们尽可能均匀地分开。

下面的脚本会有所作为 - 如果我有一个将被均匀地除以4的Count,但是如果我有一个奇数,则不会去,少于四会崩溃。您可以运行脚本,只需用您自己的文件路径替换“C:\ 1_SourceData \ Section_16 \”。如果你取消评论“向前面添加剩余部分”部分,那就是将任何额外的文件(例如奇数)发送到第一批,但这不太合适。文件夹中的文件数量范围为1到25.

任何帮助都将非常感激。

Option Explicit 
Dim fileList : Set fileList = GetFileList("C:\1_SourceData\Section_16\") 
Dim NumOfFiles : NumOfFiles = fileList.Count - 1 
Dim modNumber : modNumber = NumOfFiles/4 
Dim remainder : remainder = NumOfFiles Mod modNumber 

Dim string1 : string1 = "batch" & batchCounter 
Dim string2 : string2 = "" 

'Add remainder to front 
'Dim i : i = 0 
'For i = NumOfFiles - remainder To NumOfFiles 
' string2 = string2 & vbTab & fileList(i) & vbNewLine 
'Next 

Dim batchCounter : batchCounter = 1 
Dim file 
Dim j : j = 0 
For Each file In fileList  
string2 = string2 & vbTab & file & vbNewLine 
  j = j + 1   

If j Mod modNumber = 0 Then 
    WScript.Echo string1 & vbNewLine & string2 
    batchCounter = batchCounter + 1 
    string1 = "batch" & batchCounter 
    string2 = ""  
End If 
Next 

Public Function GetFileList(path)  
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")  
Dim fileList : Set fileList = CreateObject("System.Collections.ArrayList")  
Dim InfFolder : Set InfFolder = objFSO.GetFolder(path)  
Dim File   

For Each File In objFSO.GetFolder(path).Files  
    fileList.Add File  
    Next  Set GetFileList = fileList 
End Function

2 个答案:

答案 0 :(得分:2)

问题是:.Files集合只能通过For Each访问。 “按号码分配”(想想模数)需要一个额外的计数器。演示脚本:

Option Explicit

ReDim a(3) ' 4 groups/collections
Dim i
For i = 0 To UBound(a)
    Set a(i) = CreateObject("System.Collections.ArrayList")
Next
i = 0
Dim f
' fake a list of elms accessible via For Each only
For Each f In Split("a b c d e f g h i j k l m n")
    a(i Mod 4).Add f ' use Mod to determine the 'bucket'
    i = i + 1 ' counter needed for Mod
Next
For i = 0 To UBound(a)
    WScript.Echo i, Join(a(i).ToArray())
Next

输出:

cscript 40639293.vbs
0 a e i m
1 b f j n
2 c g k
3 d h l

答案 1 :(得分:0)

你可以用不同的方式构建你的循环。

有F文件应分别分为B批X文件。有两件事可能发生:

  1. F是B的精确倍数,在这种情况下X = F / B
  2. F不是B的精确倍数,在这种情况下X =(F / B)+ 1
  3. 因此我们可以写两个(一起)从1到F计数的循环:

    Option Explicit
    
    Const BATCHES = 4
    Const PATH = "C:\1_SourceData\Section_16"
    
    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")  
    Dim fileList : Set fileList = GetFileList(PATH)
    Dim b, i, f, x
    
    f = fileList.Count
    x = CInt(f / BATCHES)
    If x * BATCHES < f Then x = x + 1
    
    For b = 0 To BATCHES - 1
        If (b * x < f) Then WScript.Echo "batch" & (b + 1)
        For i = b * x To (b + 1) * x - 1
            If (i < f) Then WScript.Echo vbTab & fileList(i)
        Next
    Next
    
    Function GetFileList(path)
        Dim file
        Set GetFileList = CreateObject("System.Collections.ArrayList")
        For Each file In FSO.GetFolder(path).Files
            GetFileList.Add File
        Next 
    End Function