Home » 文章 » 电脑技术 » Excel » 如何将文件夹内文件名含有指定字符的文件复制到另一个文件夹

如何将文件夹内文件名含有指定字符的文件复制到另一个文件夹

如何将文件夹内文件名含有指定字符的文件复制到另一个文件夹

使用Excel的VBA代码如下:

 

Sub GetFiles()
Dim fds, fd, folser1, folder2, fs, f, mypath$, myname$
Application.ScreenUpdating = False
Application.DisplayAlerts = False
‘mypath = Replace(ThisWorkbook.Path, “\需提取的文件夹\”, “”)
mypath = “D:\需提取的文件夹绝对地址\”

Set Fso = CreateObject(“Scripting.FileSystemObject”)
Set Fso = New Scripting.FileSystemObject
Set folder1 = Fso.GetFolder(mypath) ‘获得文件夹
Set fds = folder1.Files       ‘子文件夹集合
For r = 2 To Range(“B65536”).End(xlUp).Row()
For Each f In fds                 ‘遍历文件
If f.Name Like “*” & Cells(r, 2).Value & “*” Then
‘Debug.Print f.Name
Cells(r, 3) = “完成”
‘FileCopy mypath & “\” & f.Name, “\提取结果文件夹\” & f.Name
Else
‘Cells(r, 3) = “没找到匹配文件”
End If
Next
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

做好的文件模板下载地址:分发文件夹

 

微信 OR 支付宝 扫描二维码
为博主 打个赏
pay_weixinpay_zhifubao
金额随意 快来“打”我呀~

Leave a Reply

Your email address will not be published. Required fields are marked *

*