Add Loop on Event False to exisiting script

Author Message
kriemer

  • Total Posts : 91
  • Scores: 0
  • Reward points : 0
  • Joined: 9/26/2008
  • Status: offline
Add Loop on Event False to exisiting script Wednesday, January 07, 2009 2:50 AM (permalink)
0
I currently have the following working script to test for an active internet connection and if active run some additional command code.

 Dim WshShell
 Set WshShell = createobject("wscript.shell")
 strURL = "www.yahoo.com"
 set png = WshShell.exec("ping -n 1 " & strURL)
   do until png.status = 1
 wscript.sleep 1000
 loop
 strPing = lcase(png.stdout.readall)
   Select Case True
       Case InStr(strPing, "reply from") > 1
 
 ' INSERT CODE TO RUN HERE - IF INTERNET CONNECTION ACTIVE
 
    Case Else
    End Select 
 


I would like to add a retest statement where if the "ping" fails the script waits 5 minutes then retests up to 6 more times before ending.

Not a clue how to do this.  Any suggestions?

Thanks in advance, and best for the New Year!!!



 
#1
    themcse

    • Total Posts : 171
    • Scores: 1
    • Reward points : 0
    • Joined: 12/29/2008
    • Status: offline
    RE: Add Loop on Event False to exisiting script Wednesday, January 07, 2009 3:11 AM (permalink)
    0
    You will probably want to encase your connectivity check in a function that you can use at will; your requirements logic can then be manipulated via this function call.  As a start (untested, edited in this dialog):

     Function TestConnect()
        Dim WshShell
        Set WshShell = createobject("wscript.shell")
        strURL = "www.yahoo.com"
        set png = WshShell.exec("ping -n 1 " & strURL)
        If png.status = 1 Then
            TestConnect = False
        Else
            TestConnect = True
        End If
     End Function


    And you code would be something like:

    If TestConnect Then
       ' do stuff; use a sub / function here
    Else
       WScript.Sleep 300000
       For i = 1 to 6
           If TestConnect Then ' do stuff; use a sub / function here, then exit for
       Next
       WScript.Quit
    End If

    Your case statement is an interesting way to avoid an "If" :)  You could also do the connectivity check using WMI to avoid parsing.  Regards.
    <message edited by themcse on Wednesday, January 07, 2009 3:13 AM>
     
    #2
      kriemer

      • Total Posts : 91
      • Scores: 0
      • Reward points : 0
      • Joined: 9/26/2008
      • Status: offline
      RE: Add Loop on Event False to exisiting script Wednesday, January 07, 2009 4:52 AM (permalink)
      0
      The test needs to be run within the following VBScript; where items 1 and 3 need to be tested for internet connectivity to different sites,and items 2 and 4 do not.

       Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
       objExplorer.Navigate "about: Script Running"
       objExplorer.ToolBar = 0
       objExplorer.StatusBar = 0
       objExplorer.Width=400
       objExplorer.Height = 150 
       objExplorer.Left = 0
       objExplorer.Top = 0
       objExplorer.Visible = 1
       
       objExplorer.Document.Body.InnerHTML = "Running Script... " & "Deleting Old Raw Data Close files - 1 of 4"
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       on error resume next 
       objFSO.DeleteFile("Z:\*.csv")
       on error goto 0 
       wscript.sleep 1000
       
       objExplorer.Document.Body.InnerHTML = "Running Script... " _
           & "Import data.xls - 2 of 4"
       Set xlObj = CreateObject("Excel.application")
       xlobj.Visible = True
       xlObj.Workbooks.Open "C:\plus data\Excel\21com.xla"
       xlObj.Addins("qp21com").Installed = True
       xlObj.Workbooks.Open "Z:\Raw data.xls"
       xlObj.Run "data"
       
       objExplorer.Document.Body.InnerHTML = "Running Script... " & "combine data.bat - 3 of 4"
       Set wshShell = WScript.CreateObject ("WSCript.shell")
       wshshell.run """Z:\combine.bat""", 6, True
       set wshshell = nothing
       
       objExplorer.Document.Body.InnerHTML = "Running Script... " _
           & "export data.js - 4 of 4"
       Set wshShell = WScript.CreateObject ("WSCript.shell")
       wshshell.run """Z:\export data.js""", 6, True
       set wshshell = nothing
       wscript.sleep 120000
       
       on error resume next
       objExplorer.Quit
       
       


      Sorry, but I don't know (understand) how to integrate your routine as function and sub-script calls into my code.
       
      #3
        themcse

        • Total Posts : 171
        • Scores: 1
        • Reward points : 0
        • Joined: 12/29/2008
        • Status: offline
        RE: Add Loop on Event False to exisiting script Wednesday, January 07, 2009 12:52 PM (permalink)
        0
        Was anyone else having difficulty viewing this post? Weird. This should give you a reasonable start to accomplish what you want. Of course it hasn't really been tested, and you may have to address variable scope; good luck.


         Option Explicit
         
         Dim objIE
         
         Set objIE = WScript.CreateObject("InternetExplorer.Application")
         With objIE
             '.Navigate "about: Script running"
             .Toolbar = 0
             .StatusBar = 0
             .Width = 400
             .Height = 150
             .Left = 0
             .Top = 0
             .Visible = 1
         End With
         
         If isConnected("www.yahoo.com") Then
             doStep1()
         Else
             For intCounter = 1 to 5
                 WScript.Sleep 300000
                 If isConnected("www.yahoo.com") Then
                     doStep1()
                     Exit For
                 End If
             Next
         End If
         doStep2()
         If isConnected("www.google.com") Then
             doStep3()
         Else
             For intCounter = 1 to 5
                 WScript.Sleep 300000
                 If isConnected("www.google.com") Then
                     doStep1()
                     Exit For
                 End If
             Next
         End If
         doStep4()
         
         Function isConnected(strSite)
             ' requires ICMP to be permitted
             Dim colResults, objItem, objWMI, strWQL
             isConnected = False
             Set objWMI = GetObject("WinMGMTS://./Root/CIMv2")
             strWQL = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strSite & "'"
             Set colResults = objWMI.ExecQuery(strWQL)
             For Each objItem In colResults
                 If objItem.StatusCode = 0 Then isConnected = True
             Next
         End Function
         
         Sub doStep1()
             ' put your code here
         End Sub
         
         Sub doStep2()
             ' put your code here
         End Sub
         
         Sub doStep3()
             ' put your code here
         End Sub
         
         Sub doStep4()
             ' put your code here
         End Sub
         
         
        #4
          kriemer

          • Total Posts : 91
          • Scores: 0
          • Reward points : 0
          • Joined: 9/26/2008
          • Status: offline
          RE: Add Loop on Event False to exisiting script Thursday, January 08, 2009 2:35 AM (permalink)
          0
          themcse

          I thought I sent a reasonable facsimile of the following post to you by PM but I do not see it in the out box so here goes again (as you said in the post.... Weird).

          I had to add a "Dim intcounter" statement.

          I believe that the hard part of your script works and so far as I have been able to test the script tests and re-tests for the internet connection and attempts to executes the sub-scripts "doStep#()".  My problem is that these subs fail.

          A couple of examples of sub-scripts follow:

          'objExplorer.Document.Body.InnerHTML = "Running Script... " & "Deleting Raw Data - 1 of 21"
           Set objFSO = CreateObject("Scripting.FileSystemObject")
           on error resume next 
           objFSO.DeleteFile("C:\Raw Data\*.csv")
           on error goto 0 
           wscript.sleep 1000


          and

          'objExplorer.Document.Body.InnerHTML = "Running Script... " & "Raw Data - 7 of 21"
           Set wshShell = WScript.CreateObject ("WSCript.shell")
           wshshell.run """C:\Raw Data.bat""", 6, True
           set wshshell = nothing
           wscript.sleep 10000


          For my education can you explain why you changed the "Set objExplorer = WScri..." stuff?  I presume I have to change (in the sub-scripts) the "objExplorer.Docume..." to objIE.Docume..."? Is this correct?

          Any suggestions?

          And again, thanks for your expert help.


           
          #5
            kriemer

            • Total Posts : 91
            • Scores: 0
            • Reward points : 0
            • Joined: 9/26/2008
            • Status: offline
            RE: Add Loop on Event False to exisiting script Saturday, January 10, 2009 10:24 AM (permalink)
            0
            After much PMing with themcse the following script works perfectly; (In case anyone has a similar scripting requirement):

            Option Explicit
             
             Dim objExplorer, wshShell, intcounter
             
             Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
             objExplorer.Navigate "about: Script Running"
             objExplorer.ToolBar = 0
             objExplorer.StatusBar = 0
             objExplorer.Width=400
             objExplorer.Height = 150 
             objExplorer.Left = 0
             objExplorer.Top = 0
             objExplorer.Visible = 1
             
             Function isConnected(strSite) ' requires that ICMP is active
                 Dim colResults, objItem, objWMI, strWQL
                 isConnected = False
                 Set objWMI = GetObject("WinMGMTS://./Root/CIMv2")
                 strWQL = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strSite & "'"
                 Set colResults = objWMI.ExecQuery(strWQL)
                 For Each objItem In colResults
                     If objItem.StatusCode = 0 Then isConnected = True
                 Next
             End Function
             
             If isConnected("www.yahoo.com") Then
                 doStep1()
             Else
                 For intCounter = 1 to 5
                     WScript.Sleep 5000
                     If isConnected("www.yahoo.com") Then
                         doStep1()
                         Exit For
                     End If
                 Next
             End If
             
             doStep2()
             
             If isConnected("www.google.com") Then
                 doStep3()
             Else
                 For intCounter = 1 to 5
                     WScript.Sleep 5000
                     If isConnected("www.google.com") Then
                         doStep3()
                         Exit For
                     End If
                 Next
             End If
             
             doStep4()
             
             
             Sub doStep1()
                 'insert running code here
             End Sub
             
             Sub doStep2()
                 'insert running code here
             End Sub
             
             Sub doStep3()
                'insert running code here
             End Sub
             
             Sub doStep4()
                'insert running code here
             End Sub   
             
             on error resume next
             objExplorer.Quit
             


            Regards

            K
             
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 1 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.9