Steve
Steve

Reputation: 75

Using VBS to Backup a file

I managed to find this script to use and backup my files, this is a little more complicated then I need it to be. I do not need to enter a backup path - from and too every time I run it however this is a handy option for other projects but I would like to backup from a set file path and save to a set file path to save time.

The other issue with this script is the Cancel button does not work, this is an issue I have had before and fixed but I cannot remember how to make the Cancel button function.

Option Explicit
Dim objFSO, strSourceFolder, strDestFolder, strExclusion, strPrompt

Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFolder = InputBox("Enter the Source directory path you wish to backup")

strDestFolder = InputBox("Enter the Destination directory path you wish to backup your Data to... (C:/Backup, //Backup-Server/Remotebackup")


Wscript.Echo "Click (OK) to start the Backup!"


CopyFolderStructure strSourceFolder, strDestFolder, strExclusion


Function CopyFolderStructure(strSource, strDestination, strExcludedExt)
Const OVER_WRITE_FILES = True
Dim objDir, objFolder, objFiles, strCurExt, intX, arrExt, blnExclude


Set objDir = objFSO.GetFolder(strSource)


 If Not objFSO.FolderExists(strDestination & "\" & objDir.Name) Then
    objFSO.CreateFolder(strDestination & "\" & objDir.Name)
End If

If Not IsNoData(strExcludedExt) Then

    arrExt = Split(strExcludedExt, ",")
    blnExclude = False      
End If

For Each objFiles In objFSO.GetFolder(strSource).Files

    If Not IsNoData(strExcludedExt) Then
        strCurExt = objFSO.GetExtensionName(objFiles.Name)  

        For intX = 0 To UBound(arrExt)
            If LCase(strCurExt) = arrExt(intX) Then
                blnExclude = True   
                Exit For
            Else
                blnExclude = False
            End If
        Next
        If Not blnExclude Then  
            objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES
        End If
    Else    
        objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES
    End If
Next

For Each objFolder In objFSO.GetFolder(strSource).SubFolders
    CopyFolderStructure objFolder.Path, strDestination & "\" & objDir.Name, strExcludedExt
Next
End Function

Function BrowseForFolderDialogBox(strTitle)
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = &H0001
Dim objShellApp
Dim objFolder
Dim objFldrItem
Dim objPath

Set objShellApp = CreateObject("Shell.Application")
Set objFolder = objShellApp.BrowseForFolder(WINDOW_HANDLE, strTitle , NO_OPTIONS)
If IsNoData(objFolder) Then
    WScript.Echo "You choose to cancel. This will stop this script."
    Wscript.Quit
Else
    Set objFldrItem = objFolder.Self
    objPath = objFldrItem.Path
    BrowseForFolderDialogBox = objPath
    Set objShellApp = Nothing
    Set objFolder   = Nothing
    Set objFldrItem = Nothing
End If
End Function

Function IsNoData(varVal2Check)

On Error Resume Next
If IsNull(varVal2Check) Or IsEmpty(varVal2Check) Then
    IsNoData = True
Else
    If IsDate(varVal2Check) Then
        IsNoData = False
    Elseif varVal2Check = "" Then
        IsNoData = True
    ElseIf Not IsObject(varVal2Check) Then
        IsNoData = False
    Else
        IsNoData = False
    End If
End If
End Function   
Wscript.Echo "Backup Has Completed Successfully"

Upvotes: 0

Views: 3539

Answers (1)

JosefZ
JosefZ

Reputation: 30113

Next code snippet could help (see Arguments Property (WScript Object), InputBox Function and MsgBox Function reference). Note that the Echo method behaves differently depending on which WSH engine you are using.

Option Explicit
Dim objFSO, strSourceFolder, strDestFolder, strExclusion, strPrompt

Dim iBut, sRes, sMes, objArgs
sRes = Wscript.ScriptName
sMes = vbCRLF & "(click (Cancel) button to discard)"
Set objArgs = WScript.Arguments

If objArgs.Count > 1 Then
  strSourceFolder = objArgs( 0)
  strDestFolder   = objArgs( 1) 
Else
  strSourceFolder = "C:/DataToBackup"
  strDestFolder   = "D:/Backup" 
  strSourceFolder = InputBox( "Path you wish to backup" & sMes _
    , "Source directory", strSourceFolder)
  sRes = sRes & vbNewLine & "strSourceFolder """ & strSourceFolder & """"
  If strSourceFolder = "" Then
    strDestFolder = ""
  Else
    strDestFolder = InputBox( "Path you wish to backup your Data to" & sMes _
      , "Destination directory", strDestFolder)
    sRes = sRes & vbNewLine & "strDestFolder """ & strDestFolder & """" 
  End If
End If

If strDestFolder = "" Then
  sRes = sRes & vbNewLine & "Backup Cancelled!"
  Wscript.Echo sRes
  Wscript.Quit
Else
  iBut=MsgBox(sRes & sMes, vbOKCancel + vbQuestion _
    , "Click (OK) to start the Backup!")
  If iBut <> vbOK Then Wscript.Quit
End If

''''              for debugging only: 
Wscript.Quit '''' for debugging only:
''''              for debugging only: 

Set objFSO = CreateObject("Scripting.FileSystemObject")
CopyFolderStructure strSourceFolder, strDestFolder, strExclusion

'''''' and so on...

Upvotes: 1

Related Questions