-
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
-
FilesDir = "C:\test"
Code:
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
-
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
-
-
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.