[英]Loop through folder, renaming files that meet specific criteria using VBA?
我是VBA的新手(并且只接受过一些java培训),但是在这里的其他帖子的帮助下组装了这段代码并且已经碰壁了。
我正在尝试编写循环遍历文件夹中每个文件的代码,测试每个文件是否符合某些条件。 如果满足条件,则应编辑文件名,覆盖(或删除先前)任何具有相同名称的现有文件。 然后,应将这些新重命名的文件的副本复制到其他文件夹。 我相信我非常接近,但是我的代码在运行时拒绝循环访问所有文件和/或崩溃Excel。 请帮忙? :-)
Sub RenameImages()
Const FILEPATH As String = _
"C:\\CurrentPath"
Const NEWPATH As String = _
"C:\\AditionalPath"
Dim strfile As String
Dim freplace As String
Dim fprefix As String
Dim fsuffix As String
Dim propfname As String
Dim FileExistsbol As Boolean
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
strfile = Dir(FILEPATH)
Do While (strfile <> "")
Debug.Print strfile
If Mid$(strfile, 4, 1) = "_" Then
fprefix = Left$(strfile, 3)
fsuffix = Right$(strfile, 5)
freplace = "Page"
propfname = FILEPATH & fprefix & freplace & fsuffix
FileExistsbol = FileExists(propfname)
If FileExistsbol Then
Kill propfname
End If
Name FILEPATH & strfile As propfname
'fso.CopyFile(FILEPATH & propfname, NEWPATH & propfname, True)
End If
strfile = Dir(FILEPATH)
Loop
End Sub
如果它有用,文件名将以ABC_mm_dd_hh_Page _#.jpg开头,目标是将它们剪切为ABCPage#.jpg
非常感谢!
编辑:请参阅下面的更新以获得替代解决方案。
你的代码有一个主要问题.. Loop
结束之前的最后一行是
...
strfile = Dir(FILEPATH) 'This will always return the same filename
Loop
...
以下是您的代码应该是:
...
strfile = Dir() 'This means: get the next file in the same folder
Loop
...
第一次调用Dir()
,应指定列出文件的路径,因此在进入循环之前,行:
strfile = Dir(FILEPATH)
很好。 该函数将返回与该文件夹中的条件匹配的第一个文件。 处理完文件后,如果要移动到下一个文件,则应调用Dir()
而不指定参数,以指示您有兴趣迭代到下一个文件。
=======
作为替代解决方案,您可以使用提供给VBA的FileSystemObject
类,而不是通过操作系统创建对象。
首先,通过转到Tools-> References-> Microsoft Scripting Runtime添加“Microsoft Scripting Runtime”库
如果您没有看到列出的[Microsoft Scripting Runtime],只需浏览到C:\\windows\\system32\\scrrun.dll
,它应该这样做。
其次,更改代码以使用引用的库,如下所示:
以下两行:
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
应该用这一行代替:
Dim fso As New FileSystemObject
现在运行你的代码。 如果你仍然面临一个错误,至少这次,错误应该有更多关于它的起源的细节,不像之前的模糊对象提供的通用错误。
我认为在开始处理它们之前首先收集数组或集合中的所有文件名是个好主意,特别是如果你要重命名它们。 如果您不这样做,则无法保证您不会混淆Dir(),导致它跳过文件或处理“相同”文件两次。 同样在VBA中,不需要在字符串中转义反斜杠。
以下是使用集合的示例:
Sub Tester()
Dim fls, f
Set fls = GetFiles("D:\Analysis\", "*.xls*")
For Each f In fls
Debug.Print f
Next f
End Sub
Function GetFiles(path As String, Optional pattern As String = "") As Collection
Dim rv As New Collection, f
If Right(path, 1) <> "\" Then path = path & "\"
f = Dir(path & pattern)
Do While Len(f) > 0
rv.Add path & f
f = Dir() 'no parameter
Loop
Set GetFiles = rv
End Function
如果有人想知道,这是我完成的代码。 感谢蒂姆和艾哈迈德的帮助!
Sub RenameImages()
Const FILEPATH As String = "C:\CurrentFilepath\"
Const NEWPATH As String = "C:\NewFilepath\"
Dim strfile As String
Dim freplace As String
Dim fprefix As String
Dim fsuffix As String
Dim propfname As String
Dim fls, f
Set fls = GetFiles(FILEPATH)
For Each f In fls
Debug.Print f
strfile = Dir(f)
If Mid$(strfile, 4, 1) = "_" Then
fprefix = Left$(strfile, 3)
fsuffix = Right$(strfile, 5)
freplace = "Page"
propfname = FILEPATH & fprefix & freplace & fsuffix
FileExistsbol = FileExists(propfname)
If FileExistsbol Then
Kill propfname
End If
Name FILEPATH & strfile As propfname
'fso.CopyFile(FILEPATH & propfname, NEWPATH & propfname, True)
End If
Next f
End Sub
Function GetFiles(path As String, Optional pattern As String = "") As Collection
Dim rv As New Collection, f
If Right(path, 1) <> "\" Then path = path & "\"
f = Dir(path & pattern)
Do While Len(f) > 0
rv.Add path & f
f = Dir() 'no parameter
Loop
Set GetFiles = rv
End Function
Function FileExists(fullFileName As String) As Boolean
If fullFileName = "" Then
FileExists = False
Else
FileExists = VBA.Len(VBA.Dir(fullFileName)) > 0
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.