OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for your assistance. How would I modify my code to back-up files from a specified directory to another? If the files exist, I would just like to replace them/merge with the latest one. The key here is would like to copy the entire directory path and its contents.
For example:
I would like to copy the following directory and its contents: C:\MPF\Screen Saver
to the specified directory: G:\01 HP C Drive
The final result for the back-up directory will be: G:\01 HP C Drive\MPF\Screen Saver
If that directory already exists, it would just merge the files with updated versions and any new files and directories.
This is my code, where I get the error: “Compile error Expected: =” on the following line:
Option Explicit
For example:
I would like to copy the following directory and its contents: C:\MPF\Screen Saver
to the specified directory: G:\01 HP C Drive
The final result for the back-up directory will be: G:\01 HP C Drive\MPF\Screen Saver
If that directory already exists, it would just merge the files with updated versions and any new files and directories.
This is my code, where I get the error: “Compile error Expected: =” on the following line:
VBA Code:
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(DirPth_Src, DirPth_DstNew)
VBA Code:
Public Sub MC_XX_Backup_Folders()
'_________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
'Turn off Display Alerts
Application.DisplayAlerts = False
'Turn off Screen Update
Application.ScreenUpdating = False
'Turn off Automatic Calculations
Application.Calculation = xlManual
'_________________________________________________________________________________________________
'Dimensioning
Dim i As Long
Dim LastRowCol As Long
Dim DirPth_Src As String
Dim DirPth_Dst As String
Dim DirPth_DstNew As String
'_________________________________________________________________________________________________
'Code
Sheets("Folders.Back.Up").Activate
LastRowCol = Cells(Rows.Count, 1).End(xlUp).Row
If LastRowCol < 5 Then
MsgBox "No directories to be backed-up are listed!"
Exit Sub
End If
For i = 5 To LastRowCol
DirPth_Src = Range("A" & i).Value
DirPth_Dst = Range("B" & i).Value
DirPth_DstNew = System.IO.Path.Combine(DirPth_Dst, Path.GetFileName(Path.GetDirectoryName(DirPth_Src)))
If DirPth_Src = "" Or DirPth_Dst = "" Then
MsgBox "The source or destination directory is missing in one or more lines. Please correct. This program will terminate with no changes!"
Exit Sub
End If
If Not (Directory.Exists(DirPth_DstNew)) Then
Directory.CreateDirectory (DirPth_DstNew)
End If
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(DirPth_Src, DirPth_DstNew)
Next i
'_________________________________________________________________________________________________
'Turn on alerts, screen updates, and calculate
'Turn On Display Alerts
Application.DisplayAlerts = True
'Turn on Screen Update
Application.ScreenUpdating = True
'Turn off Automatic Calculations
Calculate
'_________________________________________________________________________________________________
End Sub