excel rename file from list error

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
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Since I am not getting much help I thought I would add a little more information as it may problem may not be that clear.

My spreadsheet contains 2 columns.

Column A

file1.pdf
file2.pdf
file3.pdf


Column B
newfile1.pdf
newfile2.pdf
newfile3.pdf


Not sure what I am doing wrong here.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top