我想写一个程序,将重命名所有的文件在一个用户选择的文件夹中,其中所有的文件被命名大致根据公式“WEEKLY 00.00.00”,其中“00.00.00”<month.day.year>是这样的结果是“00_00_00”,其中两个相应的日期是相等的,多余的前缀被删除。
我发现了下面的代码,看起来可以修改它来完成这个任务:
Sub RenameFiles()
Dim xDir As String
Dim xFile As String
Dim xRow As Long
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then
xDir = .SelectedItems(1)
xFile = Dir(xDir & Application.PathSeparator & "*")
Do Until xFile = ""
xRow = 0
On Error Resume Next
xRow = Application.Match(xFile, Range("A:A"), 0)
If xRow > 0 Th
Name xDir & Application.PathSeparator & xFile As _
xDir & Application.PathSeparator & Cells(xRow, "B").Value
End If
xFile = Dir
Loop
End If
End With
End Sub
然而,我不知道如何修改它以适应我的特定需要,因为vba对我来说是如此陌生。目前,它确实允许用户确定目录(没有为我发生进一步的事件,但它没有抛出错误;是的,文件夹已填充),而且看起来像是要将旧文件名放在Excel文件的A列中,将相应文件的新名称和行值放在同一Excel文件的B列中。是这样吗?
有没有人能就这件事提供一些建设性的反馈?谢谢。
2条答案
按热度按时间qojgxg4l1#
不确定“所有文件都被命名为粗略”是什么意思,所以为了灵活起见,请考虑使用正则表达式。
vuktfyat2#
如果您希望返回一些文件名、更改它们、构建文件夹...或您想在文件系统中做的任何其他事情,使用
FileSystemObject
对新用户来说确实很有帮助。下面的代码只会将文件名从“00.00.00”更改为“00_00_00”:
如果要删除除日期以外的所有内容:
发件人:
收件人:
要添加单词: