Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

Help me stop this vbscript

I have a script that once a week it will copy a file, zip it,  and save it in a backup folder renamed with the time stamp. Then any file that is in the backup folder greater than 90 days will be deleted from the backup folder.


The script work great but it will not close. if I look at the running processes I will see all the scripts still running. Can you guys help me find away to close the script after its finished?


Thanks!



Option Explicit

Dim arrResult, strOriginalFile, strOldFileName, strExtension, strDate, strNewFile

strOriginalFile = "C:\Program Files\MessageNOW\MessageNOW Server\Backup\messagenow_backup.zip"
strOldFileName = Left(strOriginalFile, Len(strOriginalFile) - 4)
strExtension = Replace(strOriginalFile, strOldFileName, "")
strDate = TimeNow()
strNewFile = strOldFileName & "_" & strDate & strExtension

DeleteFiles "C:\Program Files\MessageNOW\MessageNOW Server\Backup\", 7

arrResult = ZipFolder( "C:\Program Files\MessageNOW\MessageNOW Server\slideshows", strNewFile )
If arrResult(0) = 0 Then
    If arrResult(1) = 1 Then
        WScript.Echo "Done; 1 empty subfolder was skipped."
    Else
        WScript.Echo "Done; " & arrResult(1) & " empty subfolders were skipped."
    End If
Else
    WScript.Echo "ERROR " & Join( arrResult, vbCrLf )
End If


Function ZipFolder( myFolder, myZipFile )

    Dim intSkipped, intSrcItems
    Dim objApp, objFolder, objFSO, objItem, objTxt
    Dim strSkipped

    Const ForWriting = 2

    intSkipped = 0

    ' Make sure the path ends with a backslash
    If Right( myFolder, 1 ) <> "\" Then
        myFolder = myFolder & "\"
    End If

    ' Use custom error handling
    On Error Resume Next

    ' Create an empty ZIP file
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    Set objTxt = objFSO.OpenTextFile( myZipFile, ForWriting, True )
    objTxt.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
    objTxt.Close
    Set objTxt = Nothing

    ' Abort on errors
    If Err Then
        ZipFolder = Array( Err.Number, Err.Source, Err.Description )
        Err.Clear
        On Error Goto 0
        Exit Function
    End If
   
    ' Create a Shell object
    Set objApp = CreateObject( "Shell.Application" )

    ' Copy the files to the compressed folder
    For Each objItem in objApp.NameSpace( myFolder ).Items
        If objItem.IsFolder Then
            ' Check if the subfolder is empty, and if
            ' so, skip it to prevent an error message
            Set objFolder = objFSO.GetFolder( objItem.Path )
            If objFolder.Files.Count + objFolder.SubFolders.Count = 0 Then
                intSkipped = intSkipped + 1
            Else
                objApp.NameSpace( myZipFile ).CopyHere objItem
            End If
        Else
            objApp.NameSpace( myZipFile ).CopyHere objItem
        End If
    Next

    Set objFolder = Nothing
    Set objFSO    = Nothing

    ' Abort on errors
    If Err Then
        ZipFolder = Array( Err.Number, Err.Source, Err.Description )
        Set objApp = Nothing
        Err.Clear
        On Error Goto 0
        Exit Function
    End If

    ' Keep script waiting until compression is done
    intSrcItems = objApp.NameSpace( myFolder  ).Items.Count
    Do Until objApp.NameSpace( myZipFile ).Items.Count + intSkipped = intSrcItems
        WScript.Sleep 200
    Loop
    Set objApp = Nothing

    ' Abort on errors
    If Err Then
        ZipFolder = Array( Err.Number, Err.Source, Err.Description )
        Err.Clear
        On Error Goto 0
        Exit Function
    End If

    ' Restore default error handling
    On Error Goto 0

    ' Return message if empty subfolders were skipped
    If intSkipped = 0 Then
        strSkipped = ""
    Else
        strSkipped = "skipped empty subfolders"
    End If

    ' Return code 0 (no error occurred)
    ZipFolder = Array( 0, intSkipped, strSkipped )
End Function

Function TimeNow()
        Dim strToday
      strToday = Year(Now)
      If Month(Now) < 10 Then
            strToday = strToday & "-0" & Month(Now)
      Else
            strToday = strToday & Month(Now)
      End If
      If Day(Now) < 10 Then
            strToday = strToday & "-0" & Day(Now)
      Else
            strToday = strToday & "-" & Day(Now)
      End If
     
   
      TimeNow = strToday
End Function

Sub DeleteFiles(strFolder, intDaysOld)
      Dim objFSO
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Dim objFile, objSubFolder, dteAgedDate
      dteAgedDate = DateAdd("d", -1*intDaysOld, Date)
      For Each objFile In objFSO.GetFolder(strFolder).Files
            If objFile.DateLastModified <= dteAgedDate Then objFile.Delete True
      Next
      For Each objSubFolder In objFSO.GetFolder(strFolder).SubFolders
            DeleteFiles objSubFolder.Path, intDaysOld
      Next

End Sub
0
kevandju
Asked:
kevandju
  • 3
  • 2
1 Solution
 
Paul JacksonCommented:
Use WScript.Quit to close in the appropriate place
0
 
Paul JacksonCommented:
You should probably set objFSO = Nothing in the function deletefiles as well
0
 
holthdCommented:
As an object is most likely to cause the delay I suggest creating a log file with timestamps that you can monitor. You can use the Sub below and insert
WriteLog "<Action description>"

Open in new window

in the different sections of your script. Narrow it down to a single a line and post it if you're unable to figure out the problem.
-Daniel
Private Sub WriteLog(byVal str_Text)
	Dim objFSO, objFile, strFile
	Set objFSO  = CreateObject("Scripting.FileSystemObject")
	If (Right(LCase(Wscript.ScriptName), 4) = ".vbs") or (Right(LCase(Wscript.ScriptName), 4) = ".wsf")Then
		strFile = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName)-4) & ".log"
	Else
		Dim objShell : Set objShell = CreateObject("Wscript.Shell")
		strFile = objShell.ExpandEnvironmentStrings("%TEMP%") & "\ScriptLog.txt"
	End If
	If objFSO.FileExists(strFile) = False Then Wscript.Echo "Log file: " & strFile
	Set objFile = objFSO.OpenTextFile(strFile, 8, True)
	objFile.WriteLine Now() & vbTab & str_Text
	objFile.Close
	Set objFile = Nothing
	Set objFSO  = Nothing
End Sub

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
kevandjuAuthor Commented:
Thanks,

Jacko
 I have tired the WScriptsQuit at the end of the Script and the scripts doent seem to even start.


thanks again
0
 
Paul JacksonCommented:
The command is WScript.Quit

Have you tried Setting the objFSO = Nothing in the DeleteFiles function. I suspect that is where your problem lies
0
 
kevandjuAuthor Commented:
Thanks, jacko72

I needed to put the WScript.Quit  after the loop  in the Zip portion of the script. works great!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now