我正在開發一個腳本,將文件夾中的文件數量分爲四組。這些將被轉換爲四個批處理文件,但現在問題是儘可能均勻地分割它們。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