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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,215,208
Messages
6,123,644
Members
449,111
Latest member
ghennedy

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