例如,我将 .png 文件更改为 .jpg
Option Explicit
' Change the Extension you want to Replace
Const CHANGE_FROM = ".png"
Const CHANGE_TO = ".jpg"
' Var Declaration - Don't Change
Dim srcFolder
Dim objFSO, objFolder, oFolder
Dim colFiles
' Set the Source Folder to Begin With or you can dynamically find this if this is being used on multiple computers
srcFolder = "C:\Temp\Files"
' Object Sets - Don't Change
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(srcFolder)
Set colFiles = objFolder.Files
' Changes Files Extensions in Source Folder
ChangeExtension colFiles
' Change the Extension for Each Sub Folder in the Source Folder
GetFilesFromSubFolders objFolder
描述:子将递归检查每个文件夹的子文件夹对于每个子文件夹,它将提取文件并使用 ChangeExtension 更改文件扩展名输入:根文件夹对象输出:每个子文件夹的递归调用 - 更改文件扩展名
--- Subs ---
Sub GetFilesFromSubFolders(objRootFolder)
Dim oSubFolders, oSubFol
Dim colSubFolFiles
' Get the Sub Folders of the Root Folder
Set oSubFolders = objRootFolder.SubFolders
' Check that Folder has Sub Folders
If oSubFolders.Count > 0 Then
' For Each Sub Folder Call Recursevlly
For Each oSubFol In oSubFolders
GetFilesFromSubFolders oSubFol
Next
End If
' Get the Files in the folder
Set colSubFolFiles = objRootFolder.Files
' Change Files Extensions in Folder
ChangeExtension colSubFolFiles
End Sub
Sub ChangeExtension(collectionSet)
' Description : Sub will Change the Extension
' for Each File that has the Requested Extension
' Input : Collection Set Object of Files
' Output : Changes the File Extension
Dim objFile
' For Each File in the Files Collection
For Each objFile In collectionSet
' Check if File has the Requested Extension
If InStr(objFile.Name,CHANGE_FROM) Then
' Checge the Extension
objFile.Name = Replace(objFile.Name,CHANGE_FROM,CHANGE_TO)
End If
Next
End Sub