my goal is to move specified folders and their contents outof an existing location into a new folder path labeled "Archive". There are about 1000 folders out of 2000 I need moved to this new location. I have a .xlsx file that contains the file paths of each folder that needs moving, listed in column A of the Excel worksheet. I would like my macro to look at the Excel file, read the folder path, move that folder and its contents to a new destination. Repeat through the Excel list until it reaches a blank, then it's considered "Done!"
Here is the code I have found so far (see below). This code will move one folder from one path to another path. I need to enhance this to read each path from my Excel file; I just don't know what that part of the command should look like.
Code and any notes with the code are greatly appreciated! Thank you!
Sub Move_Rename_Folder()
'This example move the folder from FromPath to ToPath.
Dim fso As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "Q:\Corporate-Shares\...\Test folder 1" '<< Change
ToPath = "Q:\Corporate-Shares\...\Test Archive Folder" '<< Change
'Note: It is not possible to use a folder that exist in ToPath
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If fso.FolderExists(ToPath) = True Then
MsgBox ToPath & " exist, not possible to move to a existing folder"
Exit Sub
End If
fso.MoveFolder Source:=FromPath, Destination:=ToPath
MsgBox "The folder is moved from " & FromPath & " to " & ToPath
End Sub
Please test this code in a test folder before working with your original files. Create copies or dummy files, any failure can damage your existing files....
First, separate this move function taking the name and destination of the path:
Sub Move_Rename_Folder(FromPath as string, ToPath as string)
'to do these two lines, go to tools, references and add Microsoft.Scripting.Runtime
'it's a lot easier to work like this
Dim fso As FileSystemObject
Set fso = new FileSystemObject
'you don't need to set paths anymore, they come as the arguments for this sub
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
If fso.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If fso.FolderExists(ToPath) = True Then
MsgBox ToPath & " exist, not possible to move to a existing folder"
Exit Sub
End If
fso.MoveFolder Source:=FromPath, Destination:=ToPath
MsgBox "The folder is moved from " & FromPath & " to " & ToPath
End Sub
Then, create a main Sub to run over the column "B" (from path) and Column "C" (to path), for instance:
Sub MainSub()
Dim CurrentFrom as Range, CurrentTo as Range
'get B2, assuming your B1 is a header, not a folder
Set CurrentFrom = ThisWorkbook.Worksheets("yoursheetname").Range("B2")
'get C2, assuming your C1 is a header
Set CurrentTo = ThisWorkbook.Worksheets("yoursheetname").Range("C2")
'get the actual values - paths - from cells
Dim ToPath as string, FromPath as string
ToPath = CurrentTo.value
FromPath = CurrentFrom.Value
'loop while your current frompath is not empty
Do while FromPath <> ""
'calls the move function from frompath to topath
Call Move_Rename_Folder(FromPath, ToPath)
'offsets the cells one row down
Set CurrentFrom = CurrentFrom.Offset(1,0)
Set CurrentTo = CurrentTo.Offset(1,0)
'gets the values of the new cells
FromPath = CurrentFrom.Value
ToPath = CurrentTo.Value
Loop
End Sub
Related
I have list of folder list as source path and destination path
I tried to use below codes but cannot understand how to loop in list as below one by one. I need to delete destination folders first, then copy from source folder.
c:\a
c:\b
c:\c
to
D:\a
D:\b
d:\c
Code:
Sub Copy_Certain_Files_In_Folder()
'This example copy all Excel files from FromPath to ToPath.
'Note: If the files in ToPath already exist it will overwrite
'existing files in this folder
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
FromPath = "C:\Users\Ron\Data" '<< Change
ToPath = "C:\Users\Ron\Test" '<< Change
FileExt = "*.xl*" '<< Change
'You can use *.* for all files or *.doc for Word files
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
The code that you've copied in from Ron de Bruin's Excel Automation site really deals with a whole folder. What I expect you're looking for is a loop to provide a wrapper, which provides access to the individual sub-folder first. Then you can use the code above and add your delete logic, etc. Try this (I use early binding so add reference to script library if you don't already have it.):
Option Explicit
Public Sub CopyFolders(SourceFolderName As String)
Dim fso As New FileSystemObject
Dim SourceFolder As Folder
Dim SourceSubFolder As Folder
Set SourceFolder = fso.GetFolder(SourceFolderName)
For Each SourceSubFolder In SourceFolder.SubFolders
' UPDATE AND PUT YOUR CODE TO COPY SPECIFIC FOLDERS IN HERE
Debug.Print SourceSubFolder.Name
Next
End Sub
ref: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/subfolders-property
I want to copy pdf files in one master folder to several other locations.
There is a segment of this macro where the files need to be copied to three different folders according to the state abbreviations in the file name.
Files containing AZ, CA, CO, and NM go in the "Main" folder.
Files containing FL go in the "FL" folder
All other state abbreviations go in the "Secondary" folder.
How do I list multiple state abbreviation at once and move those files.
Sub Copy_Certain_Files_In_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileName As String
FromPath = "C:\Users\Name\Desktop\Master File" '<< Change
ToPath = "C:\Users\Name\Desktop\SharePoint\Main" '<< Change
FileName = "DOC-AZ*" Or "DOC-CA*" Or "DOC-CO*" Or "DOC-NM*"
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileName, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
I don't think you can use wildcard copy in your case. If you first copy FL, AZ, CA, CO and MN to their location, they will be copied once again, since you can't make a negative wildcard for all the rest. Differently put - FL will be part of all the rest, too. Had you moved it instead, it would have worked.
Now, you can't build that kind of wildcards with FSO. Instead you will have to process them one at a time, like this (slightly simplified):
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileName As String
FromPath = "C:\Users\Name\Desktop\Master File"
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each File In FSO.GetFolder(FromPath).Files
Select Case Mid(File.Name, 5, 2)
Case "FL"
ToPath = "FL"
Case "AZ", "CA", "CO", "NM"
ToPath = "Main"
Case Else
ToPath = "Secondary"
End Select
ToPath = "C:\Users\Name\Desktop\SharePoint\" & ToPath
FSO.CopyFile FromPath & "\" & File.Name, ToPath
Next
The Select Case statement picks letter 5-6 and checks. It might be too specific for your case, but I think you get the point.
Is it possible to copy a folder to SharePoint using VBA and if so is there sample code on how this is done
Many thanks
James
You can publish an Excel file to Sharepoint (a.k.a. Chèvre-poing), but it looks rather difficult since the authentication mechanism is ill tampered (like goats).
So what you should do is map a local Share with Sharepoint (\\my_chevre_poing\goaty\, then have a little VBA do it from your favorite macro place:
Sub Copy_Folder()
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\local\trucs\" '<< your own source there
ToPath = "\\my_chevre_poing\goaty_trucs\" '<< your share there
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
You also may find huge resource there.
I have created a VBA userform, by manipulating code i found from a website, for coworkers to transfer files from selected folder from one listbox to another folder in a second listbox. The folders that populate in the listboxes change daily. It works fine for both listboxes with fmSingleSelect but i cannot figure out how to run it properly with a fmMultiSelect property on the second listbox (Yes, i changed the property to fmMultiSelect on the second listbox).
It would save time to be able to multiselect the projects folder and run the transfers simultaneously.
Below is the code for single select and commented out some code i was working with for multiselect
Also an image is below code
Thanks
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Value As String
Dim i As Integer
FromPath = "C:\Users\us-lcn-dataprep03\Desktop\Production files\" & (Me.ListBox1) '<< Change
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2) '<< Change
' For i = 0 To ListBox2.Items.Count - 1
' If ListBox2.Items(i).Selected = True Then
' Val = ListBox2.Items(i).Value
' End If
'Next i
FileExt = "*.sli*" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
Userform list boxes
The following code is a "minimal change" alteration to your code so that it should handle copying the files from one directory to multiple directories:
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Value As String
Dim i As Integer
FromPath = "C:\Users\us-lcn-dataprep03\Desktop\Production files\" & (Me.ListBox1) '<< Change
FileExt = "*.sli*" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2.List(i)) '<< Change
If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End If
Next i
End Sub
All I did was move your commented out code re looping through the selected items in ListBox2 so that it was wrapped around the parts of the code which are affected by the ToPath. (Note: The MsgBox is within the loop - you may wish to move it outside the loop but, if you do, you may want to make the message more generic - e.g. "Your files have been moved as requested".)
I also corrected some mistakes in your commented code:
ListBox2.Items.Count should be ListBox2.ListCount
ListBox2.Items(i).Selected should be ListBox2.Selected(i)
ListBox2.Items(i).Value should be ListBox2.List(i)
I only know to write a macro to copy a file from Folder A to Folder B
FileCopy "C:\Documents\Folder A\test.xlsx", "C:\Documents\Folder B\test.xlsx"
Could someone help for an Excel macro to MOVE all sub-folders and files under "Folder A" to "Folder B". Thank you
Pl Try This: Change folders as per your requirements.
Sub Copy_Folder()
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Process_Contract_Notes" '<< Change
ToPath = "C:\Process" '<< Change
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub