Created
March 16, 2019 23:07
-
-
Save lcloss/b56f336e7a8329468a1c2b2297f781a2 to your computer and use it in GitHub Desktop.
CScript: Backup one folder to another (the first is the master)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
If (WScript.Arguments.Count <> 2) Then | |
WScript.Echo("Usage: cscript BackupFolders.vbs {source} {dest}") | |
' If at least 1 argument is passed, list them | |
If (WScript.Arguments.Count > 0) Then | |
Dim strArg | |
Dim i | |
i = 0 | |
For Each strArg in WScript.Arguments | |
WScript.Echo("Argument(" & i & ") = " & strArg) | |
i = i + 1 | |
Next | |
End If | |
WScript.Quit(1) | |
End If | |
Dim objFileSys | |
Set objFileSys = CreateObject("Scripting.FileSystemObject") | |
Dim strSrcFolder | |
strSrcFolder = WScript.Arguments(0) | |
Dim strDestFolder | |
strDestFolder = WScript.Arguments(1) | |
BackupFolders strSrcFolder, strDestFolder | |
Set objFileSys = Nothing | |
WScript.Echo("End of Backup") | |
' **** End **** | |
Sub BackupFolders(strFolder1, strFolder2) | |
Dim i | |
For i = 0 To 1 | |
If i = 0 Then | |
BackupToFolder strFolder1, strFolder2 | |
Else | |
UpdateToFolder strFolder2, strFolder1 | |
End If | |
Next | |
End Sub | |
Sub BackupToFolder(strFolderSrc, strFolderDst) | |
WScript.Echo("Backup folder " & strFolderSrc) | |
' Check if source folder exists | |
If objFileSys.FolderExists(strFolderSrc) = False Then | |
WScript.Echo("Source folder does not exists: " & strFolderSrc) | |
WScript.Quit(1) | |
End If | |
' Check if destiny folder exists | |
If objFileSys.FolderExists(strFolderDst) = False Then | |
WScript.Echo("Creating " & strFolderDst) | |
objFileSys.CreateFolder(strFolderDst) | |
End If | |
Dim objFolderSrc | |
Set objFolderSrc = objFileSys.GetFolder(strFolderSrc) | |
Dim objFolderDst | |
Set objFolderDst = objFileSys.GetFolder(strFolderDst) | |
' Check Files | |
Dim objFileSrc | |
Dim objFileDst | |
For Each objFileSrc in objFolderSrc.Files | |
If Not objFileSys.FileExists(objFolderDst.Path & "\" & objFileSrc.Name) Then | |
' File does not exists | |
WScript.Echo("Copy " & objFolderSrc.Path & "\" & objFileSrc.Name & _ | |
" to " & objFolderDst.Path & "\" & objFileSrc.Name) | |
objFileSys.CopyFile objFolderSrc.Path & "\" & objFileSrc.Name, _ | |
objFolderDst.Path & "\" & objFileSrc.Name | |
Else | |
Set objFileDst = objFileSys.GetFile(objFolderDst.Path & "\" & objFileSrc.Name) | |
If (objFileSrc.DateLastModified > objFileDst.DateLastModified) Then | |
' Copy newer file | |
WScript.Echo("Override " & objFolderDst.Path & "\" & objFileDst.Name & _ | |
" with " & objFolderSrc.Path & "\" & objFileSrc.Name) | |
objFileSys.CopyFile objFolderSrc.Path & "\" & objFileSrc.Name, _ | |
objFolderDst.Path & "\" & objFileDst.Name | |
End If | |
End If | |
Next | |
' Check Subfolders | |
Dim objSubFolder | |
For Each objSubFolder in objFolderSrc.SubFolders | |
BackupToFolder objFolderSrc & "\" & objSubFolder.Name, _ | |
objFolderDst & "\" & objSubFolder.Name | |
Next | |
End Sub | |
Sub UpdateToFolder(strFolderSrc, strFolderDst) | |
WScript.Echo("Update folder " & strFolderSrc) | |
' Check if source folder exists | |
If objFileSys.FolderExists(strFolderSrc) = False Then | |
WScript.Echo("Source folder does not exists: " & strFolderSrc) | |
WScript.Quit(1) | |
End If | |
' Check if destiny folder exists | |
If objFileSys.FolderExists(strFolderDst) = False Then | |
DeleteAllFolder strFolderSrc | |
Exit Sub | |
End If | |
Dim objFolderSrc | |
Set objFolderSrc = objFileSys.GetFolder(strFolderSrc) | |
Dim objFolderDst | |
Set objFolderDst = objFileSys.GetFolder(strFolderDst) | |
' Check Files | |
Dim objFileSrc | |
Dim objFileDst | |
For Each objFileSrc in objFolderSrc.Files | |
If Not objFileSys.FileExists(objFolderDst.Path & "\" & objFileSrc.Name) Then | |
' File does not exists | |
WScript.Echo("Delete " & objFolderSrc.Path & "\" & objFileSrc.Name) | |
objFileSys.DeleteFile objFolderSrc.Path & "\" & objFileSrc.Name | |
End If | |
Next | |
' Check Subfolders | |
Dim objSubFolder | |
For Each objSubFolder in objFolderSrc.SubFolders | |
UpdateToFolder objFolderSrc & "\" & objSubFolder.Name, _ | |
objFolderDst & "\" & objSubFolder.Name | |
Next | |
End Sub | |
Sub DeleteAllFolder(strFolder) | |
' Remove all files | |
Dim objFolder | |
Set objFolder = objFileSys.GetFolder(strFolder) | |
Dim objFile | |
For Each objFile in objFolder.Files | |
WScript.Echo("Delete " & objFolder & "\" & objFile.Name) | |
objFileSys.DeleteFile objFolder & "\" & objFile.Name | |
Next | |
' Remove all subfolders | |
Dim objSubFolder | |
For Each objSubFolder in objFolder.SubFolders | |
DeleteAllFolder objFolder & "\" & objSubFolder.Name | |
Next | |
' Remove this folder | |
objFileSys.DeleteFolder strFolder | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment