[英]Copy random 25 files from 1300 to another folder with VBA
I have 1300 excel files on a server, with revenues in them. 我在服务器上有1300个excel文件,其中包含收入。 I need to compare these revenues with one pivot file to make sure the revenues are the same in the actual 2 files.
我需要将这些收入与一个数据透视文件进行比较,以确保实际2个文件中的收入相同。 Because it is on a server, opening all of them from a server would be pretty slow, thats why I want to copy a sample of them (25 excel files) to my compter first, and then run my comparison macro from this folder.
因为它在服务器上,所以从服务器打开所有文件都非常慢,这就是为什么我要首先将它们的一个样本(25个excel文件)复制到我的计算机中,然后从该文件夹运行比较宏的原因。 But I want to make the copying process automatized, so I somehow need to select randomly 25 of these files, and then copy it to an other folder.
但是我想使复制过程自动化,因此我需要以某种方式随机选择这些文件中的25个,然后将其复制到另一个文件夹中。 I have a code to copy all of the files from one folder to another, but I need the random selection to it.
我有一个代码可以将所有文件从一个文件夹复制到另一个文件夹,但是我需要对其进行随机选择。 Thanks.
谢谢。
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Users\NagyI2\Documents\Macro testing"
ToPath = "C:\Users\NagyI2\Documents\Copy test"
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
End Sub
The files
-collection of a folder
-object gives a list of files in that folder. files
- folder
集合-对象给出了该files
folder
中的文件列表。 However, you cannot access to one of the files by index, just by name. 但是,您不能按索引访问文件之一,只能按名称访问。 So the following code creates first an array with the names of all files.
因此,以下代码首先创建一个包含所有文件名称的数组。 Then, in a second loop, a file index is created by random, and the file is copied to the destination folder.
然后,在第二个循环中,随机创建文件索引,然后将文件复制到目标文件夹。
Dim FSO As Object, folder a Object, file as Object
Set folder = fso.GetFolder(FromPath)
Dim fList() As String, i As Long
ReDim fList(1 To folder.Files.Count)
For Each file In folder.Files
i = i + 1
fList(i) = file.Name
Next file
Dim copyCount As Long, fIndex As Long
copyCount = 0
Do While copyCount < 25 And copyCount < folder.Files.Count
fIndex = Int(Rnd * folder.Files.Count) + 1
If fList(fIndex) <> "" Then
Set file = folder.Files(CStr(fList(fIndex)))
file.Copy ToPath, True
fList(fIndex) = "" ' Mark this file as copied to prevent that it is picked a 2nd time
copyCount = copyCount + 1
End If
Loop
A possible solution for your task is: 您的任务的可能解决方案是:
FromPath
in an array. FromPath
中读取所有文件名。 it must be very fast 它必须非常快
Sub CopyFiles()
Dim objRows() As String
Dim fso As Object
Dim randNum As Long
Source = "C:\Users\NagyI2\Documents\Macro testing\"
Destination = "C:\Users\NagyI2\Documents\Copy test\"
randNum = 25 ' set random number
results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & Source & "*.xls*"" /S /B /A:-D").StdOut.ReadAll ' get file list in Source
objRows = Split(results, vbCrLf) ' move list to array
ReDim Preserve objRows(UBound(objRows) - 1) ' trim last empty value
sList = getRand(randNum, objRows) ' get randomized list
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
For Each sFile In sList
Call fso.CopyFile(sFile, Destination, True) ' copy randomized files
Next sFile
End Sub
Function getRand(rKey As Long, sArr As Variant) As Variant
Randomize
Set dict = CreateObject("Scripting.Dictionary")
upperbound = UBound(sArr)
lowerbound = LBound(sArr)
If rKey > upperbound Then getRand = sArr: Exit Function
For i = 1 To rKey
key = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If Not dict.Exists(key) Then dict.Add key, sArr(key) Else i = i - 1
Next i
getRand = dict.Items
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.