+ Reply to Thread
Results 1 to 17 of 17

Changing File Names in Excel Folder

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Changing File Names in Excel Folder

    snb: It worked! Thanks to everyone for their help! It was very instructional, very helpful, and most appreciated!!!
    JPDutch

    I'm wondering if someone could help me figure out how to write a macro that will allow me to change all file names in one folder (I'd like to change the year). The path is as follows:

    H:\SAFETY\FORMS\ 300 & 300A\2011

    The files look like this:

    Branch 007_2010
    Branch 008_2010
    Branch 009_2010
    Branch 010_2010

    I'd like to change the files to this:

    Branch 007_2011
    Branch 008_2011
    Branch 009_2011
    Branch 010_2011

    Any advice you could give me would be appreciated!
    Last edited by JPDutch; 12-03-2011 at 01:21 AM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Changing File Names in Excel Folder

    JPDutch,

    Something like this?
    Sub tgr()
        
        Const strFldrPath As String = "H:\SAFETY\FORMS\ 300 & 300A\2011\"
        Dim strFile As String: strFile = Dir(strFldrPath)
        
        While strFile <> vbNullString
            Name strFldrPath & strFile As Replace(strFldrPath & strFile, "2010", "2011")
            strFile = Dir
        Wend
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Changing File Names in Excel Folder

     
    Sub snb()
     For Each fl In CreateObject("scripting.filesystemobject").getfolder("H:\SAFETY\FORMS\ 300 & 300A\2011").Files
      fl.Name = Replace(fl.Name, "_2010", "_2011")
     Next
    End Sub



  4. #4
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Changing File Names in Excel Folder

    snb: Thanks for your help. This worked on the first four files and the last one, and then I got an error message: "Run-time error '58': File already exists. When I click on debug it highlights: fl.Name = Replace(fl.Name, "_2010", "_2011"). Is there something I need to correct? JPDutch

  5. #5
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Changing File Names in Excel Folder

    Tigeravatar: thanks for your help. I tried this out, but nothing happened. Any suggestions? JPDutch

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Changing File Names in Excel Folder

    1. Open the file attached.
    2. Run the macro GetFileNames and select the folder containing the files
    Path will be imported in column A
    File Names will be imported in column B
    3. Write the new file name in column C
    4. Run the macro ChangeFileNames

    This is the Macro that will get the file names & path into excel

    Sub GetFileNames()
        Dim xRow As Long
        Dim oSht As Worksheet
        Set oSht = ThisWorkbook.ActiveSheet
        Dim xDirect$, xFname$, InitialFoldr$
        xRow = oSht.UsedRange.Rows.Count + 1
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select a folder to list Files from"
            .InitialFileName = InitialFoldr$
            .Show
            If .SelectedItems.Count <> 0 Then
                xDirect$ = .SelectedItems(1) & "\"
                xFname$ = Dir(xDirect$, 7)
                Do While xFname$ <> ""
                    oSht.Range("A" & xRow) = xDirect$
                    oSht.Range("B" & xRow) = xFname$
                    xRow = xRow + 1
                    xFname$ = Dir
                Loop
            End If
        End With
        ThisWorkbook.Save
    End Sub
    And this one will rename them


    Sub ChangeFileNames()
    Dim oSht As Worksheet
    Set oSht = ThisWorkbook.ActiveSheet
    Dim i, r As Integer
    Dim strPATH As String
    Dim strOLD As String
    Dim strNEW As String
    Dim strCMD As String
    
    
    r = oSht.Cells.SpecialCells(xlCellTypeLastCell).Row
    If r < 2 Then Exit Sub
    On Error Resume Next
    With oSht
    
        For i = 2 To r
            strPATH = .Range("A" & i)
            strOLD = strPATH & .Range("B" & i)
            strNEW = .Range("C" & i)
            strCMD = "rename " & Chr(34) & strOLD & Chr(34) & " " & Chr(34) & strNEW & Chr(34)
            VBA.Shell "C:\Windows\System32\cmd.exe /c " & strCMD, vbHide
            
        Next
    End With
    
    End Sub

    Please mark the thread as SOLVED if it is.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293

    Re: Changing File Names in Excel Folder

    You can do this easily with a batch file as follows:

    From the Command Prompt (available under accessories) run the following from the directory containing the files:

    ren Branch????_2010.* Branch????_2011.*

    You change directories with the command cd DIRNAME where DIRNAME is the directory name. This can get a bit weird if you are not used to batch commands, so alternately, you can place a text file in the directory which contains the above, rename it to RENAME.BAT, and then double-click it to execute the command.

    ---GJCase

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Changing File Names in Excel Folder

    You can do that using VBA:

    sub snb()
     Shell "cmd /c ren "H:\SAFETY\FORMS\ 300 & 300A\2011\*_2010.* *_2011.*"
    End Sub

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Changing File Names in Excel Folder

    Jpdutch did you try the macro I have posted?

  10. #10
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Changing File Names in Excel Folder

    Kelshaer:

    Thank you; I did try it, and it works, but is there a way to avoid retyping? I have hundreds of these files and I'm hoping to save time and effort. Thanks again! JPDutch

  11. #11
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: Changing File Names in Excel Folder

    Check this thread out, it's exactly the same kind of question that was solved http://www.thecodecage.com/forumz/mi...lications.html
    Not all forums are the same - seek and you shall find

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Changing File Names in Excel Folder

    Here you Go.
    Run the first macro and it will pull the names as the previous one and will automatically type the new name in column C.

    Then Run the other Macro to rename them.
    Please Mark the thread as solved if it was.
    Attached Files Attached Files

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Changing File Names in Excel Folder

    Sub snb()
     For Each fl In CreateObject("scripting.filesystemobject").getfolder("H:\SAFETY\FORMS\ 300 & 300A\2011").Files
      if instr(fl.name,"_2010")>0 Then fl.Name = Replace(fl.Name, "_2010", "_2011")
     Next
    End Sub

  14. #14
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Changing File Names in Excel Folder

    Thanks snb. I'm leaving work and running home...takes about an hour. I'll try this once I get setup tonight.

  15. #15
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Changing File Names in Excel Folder

    In the GetFileNames() I'm getting a compile error: variable not defined. "i" below is the variable it highlights.

    oSht.Range("C" & xRow) = VBA.Replace(.Range("B" & i), "_2010", "_2011")

    Two questions. I've had to change the location from our H:drive to our I:drive. Do I need to make any corrections in the information you gave me? Next, I'm still using the older version of Excel: xls, not xlsm. Will that make a difference?

    Thanks in advance for your help!

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Changing File Names in Excel Folder

    Open the code and go under the second line where it says Sub GetFileNames() and type the following line:

    Dim i as integer

  17. #17
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Changing File Names in Excel Folder

    Sorry buddy for being late. You will love this one. It will ask for the folder so you can select any location. It will automatically list the new file names in column C.
    Sub GetFileNames()
        Dim xRow As Long
        Dim oSht As Worksheet
        Set oSht = ThisWorkbook.ActiveSheet
        Dim xDirect$, xFname$, InitialFoldr$
        xRow = oSht.UsedRange.Rows.Count + 1
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select a folder to list Files from"
            .InitialFileName = InitialFoldr$
            .Show
            If .SelectedItems.Count <> 0 Then
                xDirect$ = .SelectedItems(1) & "\"
                xFname$ = Dir(xDirect$, 7)
                Do While xFname$ <> ""
                    oSht.Range("A" & xRow) = xDirect$
                    oSht.Range("B" & xRow) = xFname$
                   Err.clear
                    On error resume next
                    OSht.range("C" & xRow) = vba.replace (xfname, "_2010", "_2011")
                    If err.number >0 then  oSht.Range("C" & xRow) = xFname$
                    Err.clear
                    xRow = xRow + 1
                    xFname$ = Dir
                Loop
            End If
        End With
        ThisWorkbook.Save
    End Sub
    
    
    Sub ChangeFileNames()
    Dim oSht As Worksheet
    Set oSht = ThisWorkbook.ActiveSheet
    Dim i, r As Long
    Dim strPATH As String
    Dim strOLD As String
    Dim strNEW As String
    Dim strCMD As String
    
    
    r = oSht.Cells.SpecialCells(xlCellTypeLastCell).Row
    If r < 2 Then Exit Sub
    On Error Resume Next
    With oSht
    
        For i = 2 To r
            strPATH = .Range("A" & i)
            strOLD = strPATH & .Range("B" & i)
            strNEW = .Range("C" & i)
            strCMD = "rename " & Chr(34) & strOLD & Chr(34) & " " & Chr(34) & strNEW & Chr(34)
            VBA.Shell "C:\Windows\System32\cmd.exe /c " & strCMD, vbHide
            
        Next
    End With
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1