copy a file and a folders to location based on excel list

meomo

New Member
Joined
May 13, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy a list of files and folders based on the source column in a Excel to a new location. I have an excel list of the files and folders that need to be copied and keep partial original path (See screenshot) desire outcome.


Is there anyway at all I can use a code or anything to automatically move those files and folders.

SourceDestinationDesired OutcomeComments
N:\20 - Electronic Files\1- MyAA Files\2013 fall\fileme.pdfN:\20 - Electronic Files\N:\20 - Electronic Files\2013 fall\fileme.pdfcopy this file and create the 1 level up folder
N:\20 - Electronic Files\1- MyAA Files\ET Folder\2014 fall\N:\20 - Electronic Files\N:\20 - Electronic Files\2014 fall\*.*copy this folder and all files under
N:\20 - Electronic Files\1- MyAA Files\ET Folders\2024 summer\101file.pdfN:\20 - Electronic Files\N:\20 - Electronic Files\2024 summer\101file.pdfcopy this file and create the 1 level up folder
N:\20 - Electronic Files\1- MyAA Files\ET Folders\2025 winter\N:\20 - Electronic Files\N:\20 - Electronic Files\2025 winter\*.*copy this folder and all files under

Capture11.jpeg

Thank you
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
got this far but I keep getting an error - Run-time error 52: Bad file name or number


here is my code:

Sub CopyFilesAndFolders()

Dim wb As Workbook

Dim ws As Worksheet

Dim sourceFolder As String

Dim destFolder As String

Dim fileName As String

Dim folderName As String

Dim destinationFolder As String

Dim sourcePath As String

Dim destPath As String

Dim i As Long

' Define the source folder

sourceFolder = "C:\Users\sil\Desktop\Source"


' Open the Excel file containing the list

Set wb = Workbooks.Open("C:\Users\sil\Desktop\FF2.xlsx")

Set ws = wb.Sheets("Sheet1") ' Change "Sheet1" to your sheet name


' Loop through the rows in the Excel sheet

For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

' Read values from Excel sheet

fileName = ws.Cells(i, 1).Value

folderName = ws.Cells(i, 2).Value

destinationFolder = ws.Cells(i, 3).Value



' Construct source and destination paths

sourcePath = sourceFolder & folderName & "\" & fileName

destPath = destinationFolder & folderName & "\"


' Check if destination folder exists, create if not

If Dir(destPath, vbDirectory) = "" Then

MkDir destPath

End If

' Copy file to destination

FileCopy sourcePath, destPath


Next i

' Close the Excel file

wb.Close SaveChanges:=False

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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