Macro to Copy and Rename Files in Different Directory.

calebga

New Member
Joined
Sep 23, 2009
Messages
16
Hi,

In column A is filename. I need a macro to open the file, rename it to the text listed in column B, then save it to a new location. Any takers?

Thanks in advance!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
this should move them and rename
Code:
Sub Rename_files()
oPath = "C:\test\" 'Orginal Path
nPath = "C:\results\" ' new path

For r = 2 To 4 'adjest range as needed
Name oPath & Cells(r, "A") As nPath & Cells(r, "B")
Next r
End Sub

hth,

Ross
 
Upvote 0
Thanks, Ross.

This is almost it, but it needs to copy the original file first, then move and rename. A single original file may need to be renamed several times, so the file must stay in its original location with the original name. Is that doable?

Again, I really appreciate the help!

-Caleb
 
Upvote 0
UNTESTED

Code:
Sub Rename_files()
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")

oPath = "C:\test\" 'Orginal Path
nPath = "C:\results\" ' new path

For r = 2 To 4 'adjest range as needed
fso.CopyFile(oPath & Cells(r, "A"),nPath & Cells(r, "B")
Next r
End Sub

Hth,

Ross
 
Upvote 0
Ross,

It's giving a syntax error for the following line

fso.CopyFile(oPath & Cells(r, "A"),nPath & Cells(r, "B")
 
Upvote 0
fso.CopyFile(oPath & Cells(r, "A"),nPath & Cells(r, "B")
might need to add another ) to the end
fso.CopyFile(oPath & Cells(r, "A"),nPath & Cells(r, "B") )
 
Upvote 0
Sorry, I meant to add that I'd done that already. It's still saying syntax error. Does fso need to be defined?
 
Upvote 0
lets modify the first one

Code:
Sub Rename_files()
oPath = "C:\test\" 'Orginal Path
nPath = "C:\results\" ' new path

For r = 2 To 4 'adjest range as needed
FileCopy oPath & Cells(r, "A"), nPath & Cells(r, "B")
Next r
End Sub

Ross
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,301
Members
449,149
Latest member
mwdbActuary

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