Consulting

Results 1 to 5 of 5

Thread: Rename Files

  1. #1
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location

    Rename Files

    Dear,

    I tried to rename automatically files. In Row A is the old Name and in Row b the new one. This works perfectly, but the code asks me every time to choose a folder. I just want to change this and the code should rename for example every File in the folder C:\test.

    Since 3 weeks I try to change it, but I do not find an solution. At the moment I use the code:

    Sub RenamesFiles()


    Dim FilesDir As String
    Dim CurFile As String
    Dim RowNum As Long


    With Application.FileDialog(msoFileDialogFolderPicker)


    .AllowMultiSelect = False
    If .Show = -1 Then


    FilesDir = .SelectedItems(1)
    CurFile = Dir(FilesDir & Application.PathSeparator & "*")
    Do Until CurFile = ""


    RowNum = 0
    On Error Resume Next
    RowNum = Application.Match(CurFile, Range("A:A"), 0)
    On Error GoTo 0
    If RowNum > 0 Then


    Name FilesDir & Application.PathSeparator & CurFile As _
    FilesDir & Application.PathSeparator & Cells(RowNum, "B").Value
    End If
    CurFile = Dir
    Loop
    End If
    End With
    End Sub


    Kind regards
    SFA

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    FilesDir = "C:\test"

    Option Explicit
    
    Sub RenamesFiles2()
        Dim FilesDir As String
        Dim CurFile As String
        Dim RowNum As Long
        
        FilesDir = "C:\test"
        CurFile = Dir(FilesDir & Application.PathSeparator & "*")
        
        Do Until CurFile = ""
        
            RowNum = 0
            On Error Resume Next
            RowNum = Application.Match(CurFile, Range("A:A"), 0)
            On Error GoTo 0
            If RowNum > 0 Then
            
                Name FilesDir & Application.PathSeparator & CurFile As _
                FilesDir & Application.PathSeparator & Cells(RowNum, "B").Value
            End If
            
            CurFile = Dir
        Loop
    
    
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location
    Thanks for your Reply. Now I receiving an Error in the following part of the code:

    Name FilesDir & Application.PathSeparator & CurFile As _
    FilesDir & Application.PathSeparator & Cells(RowNum, "B").Value

    Do you know what went wrong?

    Kind regards
    SFA

  4. #4

  5. #5
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location
    I just saw the macro renamed everything. I was only confused by this runtime error 13, but found the problem. With my first macro I add the names from a directory and the macro added also a hided Thumb File. When I delete this file in the list, then i do not get this error again. So it was my fault .

    My excuses und thank you for the solution.

Posting Permissions

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