News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

VBS script runs every time

Started by Magnum, December 14, 2014, 10:23:05 AM

Previous topic - Next topic

Magnum

This vbs script is supposed to only run once a day, but it runs everytime.

Anyone know how to fix it ?



'******************************************************************************
'*
'*
'* Module Name:    My Backup.vbs
'*
'* Abstract:       This is a template VB Script file generated by Reflect v5.0
'*                 Modify to add your own functionality if required
'*
'*
'******************************************************************************

OPTION EXPLICIT

' call the main function
Call VBMain()


'******************************************************************************
'* Sub:     VBMain
'*
'* Purpose: This is main function to start execution
'*
'* Input:   None
'*
'* Output:  None
'*
'******************************************************************************
Sub VBMain()
Dim objShell
Dim ExitCode

' The following function call ensures that this script only runs once a day
If HasRunToday Then
WScript.Quit
End If

Set objShell = WScript.CreateObject("WScript.Shell")

' Do the backup
ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\Reflect.exe"" -e -w <BACKUP_TYPE> ""C:\masm32\SOURCE\Reflect\My Backup.xml""")

' done
Set objShell = nothing
wscript.quit(ExitCode)
End Sub


'******************************************************************************
'* Function: Backup
'*
'* Purpose:  Calls Reflect.exe passing an XML BDF as a parameter
'*           Optionaly logs output to file
'*
'* Input:    strCmdLine Command Line Arguments
'* Output:   Exit Code
'*
'******************************************************************************
Function Backup(Byref strCmdLine)
Dim objShell
Dim objExecObject
Dim iReturnCode

strCmdLine = Replace(strCmdLine, "<BACKUP_TYPE>", GetBackupTypeParameter)

' Run the backup or image
Set objShell = WScript.CreateObject("WScript.Shell")
iReturnCode = objShell.Run(strCmdLine, 1, true)

if iReturnCode = 2 then
' Handle XML validation error

elseif iReturnCode = 1 then
' Handle backup error
elseif iReturnCode = 0 then
' Everything OK
end if
Backup = iReturnCode
Set objShell = nothing
End Function

'******************************************************************************
'* Function: HasRunToday
'*
'* Purpose:  determines if this script has run today
'*
'*
'* Input:    None
'* Output:   true if has run today false otherwise
'*
'******************************************************************************
Function HasRunToday
Dim RegScriptKey
Dim LastRunDate
Dim objShell

Set objShell = WScript.CreateObject("WScript.Shell")
RegScriptKey = "HKCU\SOFTWARE\Macrium\Reflect\Scripts\" & WScript.ScriptFullName & "\LastRun"

'Check if script has run today
ON ERROR RESUME NEXT
LastRunDate = objShell.RegRead(RegScriptKey)
If LastRunDate =  cstr(Date) Then
  HasRunToday = true
Else
objShell.RegWrite RegScriptKey, Date,"REG_SZ"
HasRunToday  = false
End If
Set objShell = nothing
End Function


'******************************************************************************
'* Function: GetBackupTypeParameter
'*
'* Purpose:  determines the backup type from command line parameter
'*           -full Full backup
'*           -inc  Incremental backup
'*           -diff Differential backup
'*
'* Input:    None
'* Output:   the parameter
'*
'******************************************************************************
Function GetBackupTypeParameter
    Dim i

    for i = 0 to Wscript.Arguments.Count - 1
        If Wscript.Arguments(i) = "-full" OR _
           Wscript.Arguments(i) = "-inc" OR _
           Wscript.Arguments(i) = "-diff" Then
            GetBackupTypeParameter = Wscript.Arguments(i)
            Exit Function
        End If
    Next

    GetBackupTypeParameter  = ""

End Function
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

GoneFishing