Use Excel 2007 to Rename Files in a Folder

Christopher_Green

New Member
Joined
Apr 28, 2013
Messages
12
Hi there,</SPAN>

When I scan documents onto our system using our scanner the scanner automatically creates the name of the file, I would like a Macro that will rename the files in a folder to the name I specify in a cell for example:</SPAN>

File Saved in: C:\My Documents</SPAN>
File Name: Scan01</SPAN>

Macro runs and asks me where the files are saved:</SPAN>

New File Name is picked up from column A and the macro renames the file.</SPAN>

Any advice or support on this would be great.</SPAN>

Thanks</SPAN>
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi Christopher,

You appear to be asking for the macro to name all the files with the same name, which can't be done because file names in a folder must be unique. Can you clarify how you envision providing unique names?

Damon
 
Upvote 0
Hi Damon,

Thanks for your Response. Sorry i was not very clear in my first Post i hope this clear's things up.

I would like a Macro that would rename a file from its orginal name to one i specify in a cell, for example:
Row/ColumnColumn AColumn BColumn C
1Current File Name:New File Name:File Location:
2Scan_0001Image_001My Documents
3Scan_0002Image_002My Documents

<TBODY>
</TBODY>

I think this should be a straight foward macro to create but my skills are no where near good enough to do this.

Best Wishes

Christopher Green
 
Upvote 0
Hi again Christopher,

This macro code should do what you describe:

Code:
Sub RenameFiles()

   Dim iRow       As Long
   Dim OldName    As String
   Dim OldNameWxt As String   'old name with extension
   Dim NewName    As String
   Dim NewNameWxt As String   'new name with extension
   Dim Ext        As String   'file extension string (including ".")
   'start with row 2 (assume row 1 is header row)
   
   iRow = 2
   Do Until IsEmpty(Cells(iRow, "A"))
      'set working folder from string in column C
      ChDir Cells(iRow, "C")
      OldName = Cells(iRow, "A")
      NewName = Cells(iRow, "B")
      'get old name including file extension
      OldNameWxt = Dir(OldName & ".*")
      Ext = Right(OldNameWxt, Len(OldNameWxt) - Len(OldName))
      NewNameWxt = NewName & Ext
      'rename file
      Name OldNameWxt As NewNameWxt
      
      iRow = iRow + 1
   Loop
      
End Sub

For this code to work properly you should use a complete path (e.g., C:\My Documents) in column C.

Note it will apply the same file extension (e.g., ".jpg") to the renamed file.
 
Upvote 0
Hi Damon,

I have been using the macro and sometimes it works but most times it doesn't.

The Run-Time Error i get is 5 "Invalid procedure or cell agrument" when i debug the macro it highlights this line
Code:
Ext = Right(OldNameWxt, Len(OldNameWxt) - Len(OldName))

I don't know if this has anything to do with it but i am renaming files that exist on network drives but i do use the file file path.

any ideas

Thanks again

Chris
 
Upvote 0
This Macro is exactly what I was looking for. I knew something like this had to be possible and I couldn't find anything in my Mr. Excel book that I thought fit. But this sounds perfect. Problem is, I have no idea how to use it!! Where do I paste this? Key this? Put this to make it work???
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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