Vba code to move subfolders and all files to different folder

kiranmalepat

New Member
Joined
Aug 5, 2014
Messages
28
I have source path list in column A and destination path list in column b in sheet 1

I want to move the files and subfolders in source path folder to destination path folder

Please help me on VBA code. Thank you 😊
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I have source folderand destination folder list in column A & Column B in excel. Looking to move subfolders and files in source folder to destination folder.

The below code is working if i directly give source path and destination path in VBA code.

Sub MoveFilesAndSubfolders()
Dim SourceFolder As String
Dim DestinationFolder As String
Dim FSO As Object
Dim Source As Object
Dim Destination As Object

' Set the source and destination folder paths
SourceFolder = "C:\SourceFolder\"
DestinationFolder = "C:\DestinationFolder\"

' Create a FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")

' Get the source folder
Set Source = FSO.GetFolder(SourceFolder)

' Loop through each file in the source folder
For Each File In Source.Files
' Move the file to the destination folder
FSO.MoveFile File.Path, DestinationFolder & File.Name
Next File

' Loop through each subfolder in the source folder
For Each Subfolder In Source.Subfolders
' Move the subfolder to the destination folder
FSO.MoveFolder Subfolder.Path, DestinationFolder & Subfolder.Name
Next Subfolder

' Clean up
Set FSO = Nothing
Set Source = Nothing
Set Destination = Nothing

MsgBox "Files and subfolders have been moved."
End Sub
-----------------------------------------------------------------------------------------

Since i have multiple source and desitination folders in excel. I am trying this. Howvever getting run time error permission denied. Please help me on this.


Sub MoveFilesAndSubfolders()

Dim SourceFolder As String

Dim DestinationFolder As String

Dim FSO As Object

Dim Source As Object

Dim Destination As Object

Dim x As Integer

For x = 0 To 100

' Set the source and destination folder paths

SourceFolder = Sheets("Folder List").Cells(x + 2, 1).Value
DestinationFolder = Sheets("Folder List").Cells(x + 2, 2).Value

If SourceFolder <> "" Then

' Create a FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")

' Get the source folder
Set Source = FSO.GetFolder(SourceFolder)
' Loop through each file in the source folder
For Each file In Source.Files
' Move the file to the destination folder
FSO.MoveFile file.PATH, DestinationFolder & file.Name
Next file

' Loop through each subfolder in the source folder

For Each SubFolder In Source.Subfolders

' Move the subfolder to the destination folder

FSO.MoveFolder SubFolder.PATH, DestinationFolder & SubFolder.Name
Next SubFolder

Else
End If

' Clean up
Set FSO = Nothing
Set Source = Nothing
Set Destination = Nothing
Next x
x = x + 1
MsgBox "Files and subfolders have been moved."
End Sub


Thank you
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top