MaryContrary
New Member
- Joined
- Jan 9, 2015
- Messages
- 2
I am not an expert nor do I have much experience with excel. Here is what I am trying to do:
I have some master template files in a directory. I want to Rename the files in the Directory but keep the old files.
I have a spreadsheet that lists the files in column A and Column B has the new name of the files. When I run the code I get an error:
File not found. My files exist and paths are ok. When I debug it points to line -> fso.CopyFile sourceFile, destFile, False
Can anyone help me?
Here is the code
Sub batch_rename()
On Error GoTo errHndl
Dim fso As New FileSystemObject
Dim fld As Folder
Dim sourcePath As String, destPath As String
Dim sourceFile As String, destFile As String, sourceExtension As String
Dim rng As Range, cell As Range, row As Range
sourcePath = "c:\test\new\"
destPath = "c:\test\new\new\"
sourceFile = ""
destFile = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set rng = ActiveSheet.Range("A1", "B6")
For Each row In rng.Rows
sourceExtension = Split(Trim(row.Cells(, 2)), ".")(1)
sourceFile = sourcePath + Trim(row.Cells(, 2))
destFile = destPath + Trim(row.Cells(, 1)) + "." + sourceExtension
fso.CopyFile sourceFile, destFile, False
Next row
MsgBox Operation was successful.", vbOKOnly + vbInformation, "Done"
Exit Sub
errHndl:
MsgBox "Error happened while working on: " + vbCrLf + _
sourceFile + vbCrLf + vbCrLf + "Error " + _
Str(Err.Number) + ": " + Err.Description, vbCritical + vbOKOnly, "Error"
End Sub
I have some master template files in a directory. I want to Rename the files in the Directory but keep the old files.
I have a spreadsheet that lists the files in column A and Column B has the new name of the files. When I run the code I get an error:
File not found. My files exist and paths are ok. When I debug it points to line -> fso.CopyFile sourceFile, destFile, False

Can anyone help me?
Here is the code
Sub batch_rename()
On Error GoTo errHndl
Dim fso As New FileSystemObject
Dim fld As Folder
Dim sourcePath As String, destPath As String
Dim sourceFile As String, destFile As String, sourceExtension As String
Dim rng As Range, cell As Range, row As Range
sourcePath = "c:\test\new\"
destPath = "c:\test\new\new\"
sourceFile = ""
destFile = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set rng = ActiveSheet.Range("A1", "B6")
For Each row In rng.Rows
sourceExtension = Split(Trim(row.Cells(, 2)), ".")(1)
sourceFile = sourcePath + Trim(row.Cells(, 2))
destFile = destPath + Trim(row.Cells(, 1)) + "." + sourceExtension
fso.CopyFile sourceFile, destFile, False
Next row
MsgBox Operation was successful.", vbOKOnly + vbInformation, "Done"
Exit Sub
errHndl:
MsgBox "Error happened while working on: " + vbCrLf + _
sourceFile + vbCrLf + vbCrLf + "Error " + _
Str(Err.Number) + ": " + Err.Description, vbCritical + vbOKOnly, "Error"
End Sub