Solved

Help me stop this vbscript

Posted on 2011-02-22
6
515 Views
Last Modified: 2012-05-11
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
Comment
Question by:kevandju
  • 3
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
Comment Utility
Use WScript.Quit to close in the appropriate place
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
You should probably set objFSO = Nothing in the function deletefiles as well
0
 
LVL 7

Expert Comment

by:holthd
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kevandju
Comment Utility
Thanks,

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


thanks again
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
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
 

Author Comment

by:kevandju
Comment Utility
Thanks, jacko72

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now