|
本帖最后由 zhaogang1960 于 2013-12-20 16:12 编辑
ailvlv 发表于 2013-12-20 15:01
感谢老师您的指导,2个问题都得到解决,但是在指定文件夹这个处理上,我没表达清楚,您现在的程序实现了替 ...
不用分两步:- Sub Macro1()
- Dim p$, f$, Fso As Object, sFileType$, i&, arr$(), brr$(), m&
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show = False Then Exit Sub
- p = .SelectedItems(1)
- End With
- sFileType = "m_*.*"
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Call GetFiles(p, sFileType, Fso, arr, m)
- If m = 0 Then
- MsgBox "没有发现以m_开头的文件!", vbInformation
- Exit Sub
- End If
- For i = 1 To m
- f = arr(1, i) & Mid(arr(2, i), 3)
- If Dir(f) <> "" Then Kill f
- Name arr(1, i) & arr(2, i) As f
- Next
- MsgBox "处理完毕", vbInformation
- Set Fso = Nothing
- End Sub
- Private Sub GetFiles(ByVal sPath$, ByVal sFileType$, ByRef Fso As Object, ByRef arr$(), ByRef m&)
- Dim Folder As Object
- Dim SubFolder As Object
- Dim File As Object
- Set Folder = Fso.GetFolder(sPath)
- For Each File In Folder.Files
- If File.Name Like sFileType Then
- If File.Name <> ThisWorkbook.Name Then
- m = m + 1
- ReDim Preserve arr(1 To 2, 1 To m)
- arr(1, m) = sPath & ""
- arr(2, m) = File.Name
- End If
- End If
- Next
- If Folder.SubFolders.Count > 0 Then
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder.Path, sFileType, Fso, arr, m)
- Next
- End If
- Set Folder = Nothing
- Set File = Nothing
- Set SubFolder = Nothing
- End Sub
复制代码
|
|