"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

May 06, 2012

File Backup Copy Script

A quick task came on my plate. Task has to perform

  • File Copy across Network Drive
  • Delete Old Archived Files

Following posts provided good direction to get started

Access network share from within VBScript eg FileSystemObject
VBScript: Delete Old Files and Folders

Final Script was logic from both posts


ServerShare = "\\IPAddress\c$\Files"
UserName = "SystemName\UserName"
Password = "Password"
NumberOfDays = 3

Set NetworkObject = CreateObject("WScript.Network")
NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password

Set objFS = CreateObject("Scripting.FileSystemObject")
strSourceFolder = "Drive\SourceFolder"
Set strDestination = objFS.GetFolder(ServerShare)
Set objFolder = objFS.GetFolder(strSourceFolder)

'Delete Files Older than X Days
For Each objFile In objFolder.files
    If DateDiff("d", objFile.DateCreated,Now) > NumberOfDays Then
       WScript.Echo objFile
        objFile.Delete True
        End If
Next

For Each objFile In strDestination.files
    If DateDiff("d", objFile.DateCreated,Now) > NumberOfDays Then
       WScript.Echo objFile
        objFile.Delete True
        End If

Next
Go(objFolder)
Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders      
        Go eFolder
    Next
    For Each strFile In objDIR.Files
        strFileName = strFile.Name
        WScript.Echo strFileName
       objFS.CopyFile strFile , strDestination &"\"& strFileName
    Next   
  End If 
End Sub

Set Directory = Nothing
Set FSO = Nothing
Set ShellObject = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False
Set NetworkObject = Nothing


Happy Learning!!!

No comments: