PDA

View Full Version : Rename Files



SFA
03-27-2018, 04:39 AM
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

mana
03-27-2018, 05:04 AM
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

SFA
03-27-2018, 05:37 AM
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

mana
03-27-2018, 06:18 AM
https://msdn.microsoft.com/en-us/library/aa232676%28v=vs.60%29.aspx?f=255&MSPPError=-2147217396

SFA
03-27-2018, 06:27 AM
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.