VB and VBA Users Source Code: Backing up a directory (and sub directories) using the XCopy command
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Backing up a directory (and sub directories) using the XCopy command
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, September 12, 2005
Hits:
2698
Category:
Files/Directories/IO
Article:
Below is a routine for backing up a directory into another diretory. The routine calls the XCopy command to perform the backup. Example code showing how to call the routine can be found at the bottom of this post. 'Purpose : Backups up a directory (and optionally sub directories). 'Inputs : sSourceDirectory The directory to backup. ' sDestinationDirectory The destination directory to store the backup. ' [sFilePattern] The files to backup. ' [bIncludeSubDirs] If will backup sub directories, else just backs ' up the source directory. 'Outputs : N/A 'Author : Andrew Baker 'Date : 25/03/2005 'Notes : Backs up files that have changed. Sub DirectoryBackup(ByVal sSourceDirectory As String, ByVal sDestinationDirectory As String, Optional sFilePattern As String = "*.*", Optional bIncludeSubDirs As Boolean = True) Dim sParams As String If Right$(sSourceDirectory, 1) <> "\" Then sSourceDirectory = sSourceDirectory & "\" End If If Right$(sDestinationDirectory, 1) <> "\" Then sDestinationDirectory = sDestinationDirectory & "\" End If 'Quote the paths sSourceDirectory = """" & sSourceDirectory & sFilePattern & """" sDestinationDirectory = """" & sDestinationDirectory & """" 'Determine parameters sParams = " /Y /C /Q /D" If bIncludeSubDirs Then sParams = sParams & " /E" End If 'Backup the path Shell "xcopy " & sSourceDirectory & " " & sDestinationDirectory & sParams, vbMinimizedNoFocus End Sub Sub Test() DirectoryBackup "C:\Test", "C:\Backup" End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder