Senthil Murugan
New Member
- Joined
- Sep 25, 2024
- Messages
- 47
- Office Version
- 365
- Platform
- Windows
Good Evening Everybody
Can anybody help me to Move files from folder to another folder based on listbox file names Selected
Private Sub CommandButton1_Click()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder As String
Dim destinationFolder As String
Dim selectedFile As String
Dim selectedItemIndex As Integer
sourceFolder = "F:\"
destinationFolder = "E:\"
If Not fso.FolderExists(destinationFolder) Then
fso.CreateFolder destinationFolder
End If
If ListBox1.ListIndex <> -1 Then
For selectedItemIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(selectedItemIndex) Then
selectedFile = ListBox1.List(selectedItemIndex)
If fso.FileExists(sourceFolder & selectedFile) Then
fso.MoveFile sourceFolder & selectedFile, destinationFolder & selectedFile
MsgBox "File " & selectedFile & " has been moved to " & destinationFolder, vbInformation
Else
MsgBox "File not found: " & selectedFile, vbExclamation
End If
End If
Next selectedItemIndex
Else
MsgBox "No files selected", vbExclamation
End If
Set fso = Nothing
End Sub
Thanks in advance
A.Senthil Murugan
Can anybody help me to Move files from folder to another folder based on listbox file names Selected
Private Sub CommandButton1_Click()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder As String
Dim destinationFolder As String
Dim selectedFile As String
Dim selectedItemIndex As Integer
sourceFolder = "F:\"
destinationFolder = "E:\"
If Not fso.FolderExists(destinationFolder) Then
fso.CreateFolder destinationFolder
End If
If ListBox1.ListIndex <> -1 Then
For selectedItemIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(selectedItemIndex) Then
selectedFile = ListBox1.List(selectedItemIndex)
If fso.FileExists(sourceFolder & selectedFile) Then
fso.MoveFile sourceFolder & selectedFile, destinationFolder & selectedFile
MsgBox "File " & selectedFile & " has been moved to " & destinationFolder, vbInformation
Else
MsgBox "File not found: " & selectedFile, vbExclamation
End If
End If
Next selectedItemIndex
Else
MsgBox "No files selected", vbExclamation
End If
Set fso = Nothing
End Sub
Thanks in advance
A.Senthil Murugan