I have scenario where I need to move files to another location based on partial file name. For eg., "FAI 741727-001 SMS CQ 6U PASS 061217.xlsx" is the file name and I want to create another location as 6U and then move this file to that folder.
I have a code which helps me to move the file to a folder only if I give the full file name. Can someone please help me on this..
Code:
Sub MoveFiles()
Dim SourcePath As String
Dim DestPath As String
Dim FileName As String
Dim LastRow As Long
Dim i As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
FileName = Cells(i, "B").Value
If Right(Cells(i, "A").Value, 1) <> Application.PathSeparator Then
SourcePath = Cells(i, "A").Value & Application.PathSeparator
Else
SourcePath = Cells(i, "A").Value
End If
If Right(Cells(i, "C").Value, 1) <> Application.PathSeparator Then
DestPath = Cells(i, "C").Value & Application.PathSeparator
Else
DestPath = Cells(i, "C").Value
End If
If Dir(SourcePath & FileName) = "" Then
Cells(i, "D").Value = "Source file does not exist."
ElseIf Dir(DestPath & FileName) <> "" Then
Cells(i, "D").Value = "File already exists."
Else
Name SourcePath & FileName As DestPath & FileName
Cells(i, "D").Value = "File moved to new location"
End If
Next i
End Sub

