Hi,

i want to clean up my desktop and move all the files in my desktop to defined folder. can i get a macro code which will perform this action.

i have a code which will copy the files and move it to defined destination, but i don't want to keep the folder in older directory.


Option Explicit

Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Const FO_MOVE As Long = &H1
Private Const FOF_SIMPLEPROGRESS As Long = &H100

Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type

Sub Sample()
Dim fileToOpen As Variant
Dim outputfolder As String
Dim i As Long

outputfolder = "C:\Users\A467599\Desktop\To folder"

fileToOpen = Application.GetOpenFilename(MultiSelect:=True)



If IsArray(fileToOpen) Then
'If Dir(outputfolder) = "" Then MkDir outputfolder

For i = LBound(fileToOpen) To UBound(fileToOpen)
Call VBCopyFolder(fileToOpen(i), outputfolder)
Next i
Else
MsgBox "No files were selected."
End If
End Sub

Private Sub VBCopyFolder(ByRef strSource, ByRef strTarget As String)
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_MOVE
.pTo = strTarget
.pFrom = strSource
.fFlags = FOF_SIMPLEPROGRESS
End With
'~~> Perform operation
SHFileOperation op
End Sub

Thanks,
Uday