使用通配符将文件列表从一个目录移动到另一个目录

时间:2018-11-16 11:43:37

标签: excel vba excel-vba

我实际上已经找到了解决方案,但是快速浏览后却找不到我想要的解决方案,所以我想与大家分享并希望获得一些改进建议或您可以想到的其他有用功能使其变得更强大/更友好的用户。

我只需在要搜索的文件名中添加一个星号就可以使用通配符。 “ hell *”将找到“ hello.pdf”并将其复制到新的Dir:D

我遇到的一个问题;似乎不喜欢我们的共享网络驱动器在工作,假设在bg中可能存在某种权限,以防止此类脚本造成任何损害。

Sub moveFilesList()

Dim searchRange As Range
Dim cell As Range
Dim fn As String
Dim myDir As String
Dim newFolder As String
Dim continue As Long
Dim LR As Long

MsgBox ("You have initiated the File Moving Macro!")
myDir = InputBox("Enter the filepath for your Source Directory", "Input DIR")
newFolder = InputBox("Enter the filepath for your Destination Directory", "Output DIR")
continue = MsgBox("All cells in Column A will be searched for!" & vbNewLine & "Continue?", vbOKCancel)

If continue = 2 Then Exit Sub

With ActiveSheet
    LR = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set searchRange = .Range("A1:A" & LR)
End With
For Each cell In searchRange
    fn = Dir(myDir & "\" & cell)
    If fn <> "" Then FileCopy myDir & "\" & fn, newFolder & "\" & fn
Next cell

End Sub

0 个答案:

没有答案