greetings,
as the title says, i'm trying to rename all files from a base path and files from its subfolders.
my code, so far,.. only renames files in the base folder..
and it has an error path/file access error...
what it currently does is it renames all files in the base folder that has "14" and changes the "14" to "15" or whatever the variable's value is.
what needs to be done is to rename all the files including the ones inside the subfolders.
ex:
fv(from value)=14
tv(to value) =15
files on base folder (C:\Test\)
Luis14_OS.xls to Luis15_OS.xls
Bong14_OS.xls to Bong15_OS.txt
Gina14_OS.xls to Gina15_OS.docx
June14 (This is a folder)
inside june 14, (C:\Test\June14\)
Marcus14.iso to Marcus15.iso
ouch_14a.xlsm to ouch_15a.xlsm
Regards,
Thanks in advance! =)
as the title says, i'm trying to rename all files from a base path and files from its subfolders.
my code, so far,.. only renames files in the base folder..
and it has an error path/file access error...
Code:
Public Sub rFiles()Dim strPath As String
Dim lngCount As Long
Dim position As Integer
Dim fso As FileSystemObject
Dim file_ As File
path = Range("A2").Value
fv = Range("B2").Value
tv = Range("C2").Value
strPath = path & "\"
strFile = Dir(strPath & "*" & fv & "*")
Set fso = New FileSystemObject
If (Not (fso.FolderExists(path))) Then
'the folder path is invalid. Exiting.
MsgBox "Invalid Path"
Exit Sub
End If
fileCounter = 1
'Set activeSht = ActiveSheet
Set baseFolder = fso.GetFolder(path)
For Each file_ In baseFolder.Files
position = InStrRev(strFile, ".")
suffix = Right(strFile, Len(strFile) - (position - 1))
Filename = Left(strFile, Len(strFile) - Len(suffix))
newFilename = Replace(Filename, fv, tv)
lngCount = lngCount + 1
strName = newFilename & suffix
Name strPath & strFile As strPath & strName
strFile = Dir
Next
End Sub
what it currently does is it renames all files in the base folder that has "14" and changes the "14" to "15" or whatever the variable's value is.
what needs to be done is to rename all the files including the ones inside the subfolders.
ex:
fv(from value)=14
tv(to value) =15
files on base folder (C:\Test\)
Luis14_OS.xls to Luis15_OS.xls
Bong14_OS.xls to Bong15_OS.txt
Gina14_OS.xls to Gina15_OS.docx
June14 (This is a folder)
inside june 14, (C:\Test\June14\)
Marcus14.iso to Marcus15.iso
ouch_14a.xlsm to ouch_15a.xlsm
Regards,
Thanks in advance! =)