Solved

Batch Rename Files from Excel using VBA

Posted on 2013-06-12
8
551 Views
Last Modified: 2013-06-14
Dear Experts :

I need to do some renaming by using an Excel makro

Column A: All files listed there are to be renamed. They are all located in C:\Test\
Column B: The new names are to be taken from Column B

I have attached a sample file for your convenience.

Help is much appreciated. thank you very much in advance.

Regards, Andreas

Batch-Renaming-Files.xls
0
Comment
Question by:AndreasHermle
  • 4
  • 2
  • 2
8 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 39242574
Use this macro:

Public Sub RenameFiles()

    Dim Cell As Range
   
    With ActiveSheet
        For Each Cell In Intersect(.Range("A:A"), .UsedRange).Cells
            If Cell.Value <> "" Then
                Name "C:\Test\" & Cell.Value As "C:\Test\" & Cell.Offset(0, 1).Value
            End If
        Next Cell
    End With

End Sub

Kevin
0
 

Author Comment

by:AndreasHermle
ID: 39242600
HI Kevin,

thank you very much for your quick help.

I am afraid to tell that your code throws an error message (Runtime Error 5) on ...

 Name "C:\Test\" & Cell.Value As "C:\Test\" & Cell.Offset(0, 1).Value

Any idea why?

regards, Andreas
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39242610
That's an access denied error. Do you have security rights to that folder? Is one of the files open?

Kevin
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 17

Accepted Solution

by:
andrewssd3 earned 250 total points
ID: 39242614
As coded this needs a reference to the Microsoft Scripting Runtime.

Public Sub RenameFiles()

    Dim fso As Scripting.FileSystemObject
    Dim vFiles As Variant
    Dim i As Long
    Dim oFile As Scripting.File
    Dim sFilePath As String
    
    Const cFILE_PATH As String = "C:\Temp"
    
    Set fso = New Scripting.FileSystemObject
    
    vFiles = ActiveSheet.Cells(1).CurrentRegion.Value
    
    For i = 1 To UBound(vFiles, 1)
    
        With fso
            sFilePath = .BuildPath(cFILE_PATH, vFiles(i, 1))
            If .FileExists(sFilePath) Then
                Set oFile = .GetFile(sFilePath)
                
                oFile.Name = vFiles(i, 2)
                
            End If
        End With
    
    Next i

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39242617
Sorry - a lot has happened since I started typing that last solution! Must refresh more often
0
 

Author Comment

by:AndreasHermle
ID: 39242735
Dear both,

thank you very much for your great help.

I am off to bed now and will do the testing and troubleshooting tomorrow.

Again, thank you very much for help

Regards, andreas
0
 

Author Comment

by:AndreasHermle
ID: 39249613
Ok, both codes work just fine. Thank you very much for your great job.

I will award points now, I suggest splitting the points equally and then post another question to also include a folder picker in the codes.

Again, thank you very much for your professional and swift help.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39249615
Acutally I cannot say which code is better since both work fine but since the button next to the 'best solution' is a radio button I am not unable to uncheck it.

Thank you very much for your great support. I really appreciate it.

Regards, Andreas
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question