好的,我试着添加一些评论给你一些方向。你遇到的第一个问题是你没有对根文件夹做任何事情——你试图直接进入子文件夹,这可能就是你说它“突出显示”外循环层上的行的原因。(突出显示的行是您接下来按 F8 时将执行的行。)
我所做的是将复制操作分解为另一个过程,以便您可以在任何子文件夹上递归调用它。这只是一种方法 - 还有其他可能更简单的方法,但这是我想到的,因为我有点习惯于以这种方式递归地挖掘文件夹和记录集。
您遇到的另一个问题是比较日期的方法。属性的格式.DateCreated
带有日期和时间。您可以直接将其与Now()
返回日期和时间的函数进行比较 - 但如果您尝试与该Date()
函数进行比较,它将无法正常工作,因为它是一种不同的格式。
我不确定您要对文件扩展名位做什么。我假设您想将其用作过滤器,所以我就是这样做的。
一些注意事项:您目前在最后告诉用户“您可以从中找到文件”,但您没有检查这是否属实。您可能希望在.Copy
操作后添加检查,然后将结果添加到数组或其他内容中,以便向用户显示成功复制的文件和未复制的文件的列表。在测试时,我创建了您在我的Users
目录中拥有的文件夹,但在尝试复制没有所需权限时出现错误。
现在 From 路径、To 路径和扩展过滤器都是硬编码的。如果您打算分发它或将在多个位置自己使用它,您可以使用BrowseForFolder方法向用户显示文件夹浏览器对话框,并允许他们选择 From 和 To 文件夹。您还可以使用InputBox
从用户那里获取过滤器。只是一个想法。
无论如何,这就是我对你的代码所做的。我将变量名称更改为我的命名约定只是因为这是我习惯的 - 您可以根据需要更改它们。
Option Explicit
Public Sub CopyPasteFiles()
'Declare variables
Dim SRfso As Scripting.FileSystemObject
Dim strFrom As String
Dim strTO As String
Dim strExtFilter As String
Dim SRfolderA As Scripting.Folder
Dim SRfolderB As Scripting.Folder
'Are you always going to hardcode these or do you want to be able to browse for a folder?
strFrom = "C:\Users\Run" '<< Change
strTO = "C:\Users\Test" '<< Change
'I'm not sure what your intent is with this - I assumed you wanted to filter by file extension.
strExtFilter = "*BT.CSV"
'Prep the folder path
If Right(strFrom, 1) <> "\" Then
strFrom = strFrom & "\"
End If
'Intialize the FileSystemObject
Set SRfso = New Scripting.FileSystemObject
'Verify input and output folders exist. Inform user if they don't.
If SRfso.FolderExists(strFrom) = False Then
MsgBox strFrom & " doesn't exist"
Exit Sub
End If
If SRfso.FolderExists(strTO) = False Then
MsgBox strTO & " doesn't exist"
Exit Sub
End If
'Get the input folder using the FileSystemObject
Set SRfolderA = SRfso.GetFolder(strFrom)
'Call the routine that copies the files
MoveTheFiles SRfolderIN:=SRfolderA, strFolderOUT:=strTO ', strExtFilter:=strExtFilter
'Inform the user where they can find the files. CAUTION: You may be misinforming the user.
MsgBox "You can find the files from " & strFrom & " in " & strTO
End Sub
Private Sub MoveTheFiles(ByRef SRfolderIN As Scripting.Folder, _
ByRef strFolderOUT As String, _
Optional ByRef strExtFilter As String = "*.*", _
Optional ByRef blnSUBFOLDERS As Boolean = True)
'This routine copies the files. It requires two arguments. First, it requires the root folder as folder object from the scripting library. _
Second, it requires the output path as a string. There are two optional arguments. The first allows you _
to use a text filter as a string. The second is a boolean that tells us whether or not to move files in subfolders - the default is true.
'Delcare variables
Dim SRfileA As Scripting.File
Dim SRfolderCol As Scripting.Folders
Dim SRfolderA As Scripting.Folder
Dim datCreated As Date
Dim lngFX As Long
Dim blnResult As Boolean
'Find the file extension in the filter
lngFX = InStrRev(strExtFilter, ".", , vbTextCompare)
'Move the files from the root folder
For Each SRfileA In SRfolderIN.Files
'Only work with files that contain the filter criteria
If Ucase(Mid(SRfileA.Name, InStrRev(SRfileA.Name, ".", , vbTextCompare) - (Len(strExtFilter) - lngFX) + 1, Len(strExtFilter))) Like Ucase(strExtFilter) Then
'Only work with files that were created within the last 100 days
datCreated = SRfileA.DateCreated
If datCreated <= Now And (datCreated >= DateAdd("d", -100, Now())) Then
SRfileA.Copy strFolderOUT
End If
End If
Next
'Check if the calling procedure indicated we are supposed to move subfolder files as well
If blnSUBFOLDERS Then
'Check that we have subfolders to work with
Set SRfolderCol = SRfolderIN.SubFolders
If SRfolderCol.Count > 0 Then
For Each SRfolderA In SRfolderIN.SubFolders
MoveTheFiles SRfolderIN:=SRfolderA, strFolderOUT:=strFolderOUT, strExtFilter:=strExtFilter, blnSUBFOLDERS:=blnSUBFOLDERS
Next
End If
End If
End Sub