Solved

Help me stop this vbscript

Posted on 2011-02-22
6
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 29

Accepted Solution

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

Expert Comment

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

Expert Comment

by:holthd
ID: 34955026
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kevandju
ID: 34955029
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
ID: 34955055
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
ID: 34955831
Thanks, jacko72

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

730 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