Hi Colin
Thanks for your reply
Now it should also move files without file extensions.
It also test if Source path is equal to Destination path. If that is the
case the user will be notified and the macro stop.
Sub test1()
Range("B2", Range("B2").End(xlDown)).ClearContents
Set fs = CreateObject("Scripting.FileSystemObject")
SourcePath = InputBox("Enter source path : ")
If SourcePath = "" Then Exit Sub
sExists:
If fs.FolderExists(SourcePath) = False Then
SourcePath = InputBox("The path " & SourcePath & " don't exists" _
& vbLf & vbLf & "Enter path : ")
GoTo sExists
End If
If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
DestPath = InputBox("Enter destination path : ")
If DestPath = "" Then Exit Sub
dExists:
If fs.FolderExists(DestPath) = False Then
DestPath = InputBox("The path " & DestPath & " don't exists" _
& vbLf & vbLf & "Enter path : ")
GoTo dExists
End If
If Right(DestPath, 1) <> "\" Then DestPath = DestPath & "\"
If SourcePath = DestPath Then
msg = MsgBox("Source path is equal to destionation path" & _
vbLf & vbLf & "Ending macro !", vbCritical, "Warning !")
Exit Sub
End If
LastRow = Range("A1").End(xlDown).Row
For r = 2 To LastRow
FileToMove = SourcePath & Cells(r, "A").Value
If fs.GetExtensionName(FileToMove) = "" Then
With Application.FileSearch
.NewSearch
.LookIn = SourcePath
.SearchSubFolders = False
.Filename = FileToMove
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
For c = 1 To .FoundFiles.Count
TargetFile = .FoundFiles(c)
ext = fs.GetExtensionName(TargetFile)
If Not IsNumeric(ext) Then
FileToMove = FileToMove & "." & ext
End If
If fs.fileexists(FileToMove) = True Then
fs.MoveFile FileToMove, DestPath
Cells(r, "B") = "Moved"
Exit For
Else
Cells(r, "B") = "Not Found In Source Path"
End If
Next
Else
Cells(r, "B") = "Not Found In Source Path"
End If
End With
Else
Debug.Print fs.GetExtensionName(FileToMove) & " " & FileToMove
If fs.fileexists(FileToMove) = True Then
fs.MoveFile FileToMove, DestPath
Cells(r, "B") = "Moved"
Else
Cells(r, "B") = "Not Found In Source Path"
End If
End If
Next
Set fs = Nothing
Columns("B:B").EntireColumn.AutoFit
End Sub
Regards,
Per