Link to home
Start Free TrialLog in
Avatar of Nechako91
Nechako91

asked on

Involved circular VBScript

I am trying to create a VBScript that does a few things. In order these are:

1. Open a command prompt
2. Start logging "netstat 1" information and writing it to a logfile
3. Open a browser window and browse to a certain site
4. Wait 15 sec
5. Close Browser
6. End "netstat 1" capture

I would like to do this whole series of steps repeatedly and every time have it automatically increment the logfiles name by 1. So far I have code snippets in no particular order that will accomplish steps 1,2,3,4,6. I am missing step 5 and can not figure out how to add in a variable to my file names so that I can loop this whole process.

A couple snippets:
Call ViewHtml( "www.website.com", False )
'******************************************************************************
'The following sub function is from tjackson @ TechRepublic
'This function makes a call to Internet Explorer and opens whichever site is
'passed to it as a variable.
Sub ViewHtml( ByVal sFile, ByVal bWait )
Dim oWsh
Const sgExeFile = "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
Const sWINDOWSTYLE = 3

If bWait = Empty Then
bWait = False
End If

Set oWsh = WScript.CreateObject("WScript.Shell")
oWsh.Run CHR(34) & sgExeFile & CHR(34) & " " & sFile, sWINDOWSTYLE, bWait

Set oWsh = Nothing
End Sub
'******************************************************************************
My call to do the log output looks like:
objShell.Run "%comspec% /k netstat 1 > data1.txt"
Where data1.txt would need to have the number replaced by an incremental variable.

Probably confusing but thanks for any help on this!
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, try this, which so far doesn't involve a looping process, but will do steps 1 to 6.

'===============
' Parts 1 and 2 - output netstat to log file
' Please keep the three digits on the end of the file name, so the
' increment knows how many digits there are
strLogFile = "data001.txt"
strFileName = Left(strLogFile, InStrRev(strLogFile, ".") - 1)
strExtension = Mid(strLogFile, InStrRev(strLogFile, "."))
Set objFSO = CreateObject("Scripting.FileSystemObject")
While objFSO.FileExists(strFileName & strExtension) = True
      strFileName = Left(strFileName, Len(strFileName) - 3) & Right("000" & Int(Right(strFileName, 3)) + 1, 3)
Wend
Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c netstat 1 > " & strFileName & strExtension
Set objExec = objShell.Exec(strCommand)
'objShell.Run strCommand, 1, False

' Part 3 - Open browser to a specific site
' Set up an objIE object to open a browser to a specific site
strURL = "http://www.google.com"
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
        .left=200
        .top=200
        .height=400
        .width=400
        .menubar=0
        .toolbar=1  
        .statusBar=0
        .navigate(strURL)
        .visible=1
End With

' Wait a while until IE as finished to load
Do While objIE.busy
      WScript.Sleep 100
Loop

' Part 4 - Wait 15 seconds if required
' However, if the browser is busy during processing, and you only
' need to wait for it to finished, the above objIE.Busy state should
' cover that, and this sleep won't be required.
WScript.Sleep 15000

' Part 5 - Close the browser
objIE.Quit
Set objIE = Nothing

' Part 6 - Terminate the netstat capture
objExec.Terminate
Set objExec = Nothing

' Clean up
Set objShell = Nothing
Set objFSO = Nothing

MsgBox "Done"
'===============

Regards,

Rob.
Avatar of Nechako91
Nechako91

ASKER

Wow, I took a quick glance at this and I like what I see. I will need a day or two to play with this new information and get it tested out but I think this may be the solution that I am looking for. Thanks! I will let you know soon and accept answer once done.

A quick note that you are right about the sleep 15 seconds, it wont be necessary. That was my rough estimate to how long it was taking to load the page I am testing.

Thanks again!
Great, no worries.  Take you time.  Let me know if you need anything else...like a loop through multiple sites or something....

Regards,

Rob.
Well the script runs great now with a few last minor probles to be worked out. It seems like my captures are being caused to sleep as well when I do a WScript.Sleep, because my .txt files I am getting will only show 2 or 3 seconds worth of data from a process that is taking 45 sec, due to sleeps. The other thing that isn't happening, the captures are not closing out properly and continue to run, which in turn seems to make the script continue running. Unless I manually kill the script process, it would sit and continue to collect data.

Do you have an email where I could send a copy of the script? Or should I just post what I have again?

Thanks!
You can either post the script contents here, or you can upload the file to www.ee-stuff.com

I'll do some more testing....

Rob.
'======================================================================
' Testing Script
' Oct 3, 2007
' Tyrell M., help provided by Rob Sampson from ExpertsExchange

Dim iter

iter = 0

Do While iter < 2


' Please keep the three digits on the end of the file name, so the
' increment knows how many digits there are
strLogFile = "data001.txt"
strFileName = Left(strLogFile, InStrRev(strLogFile, ".") - 1)
strExtension = Mid(strLogFile, InStrRev(strLogFile, "."))
Set objFSO = CreateObject("Scripting.FileSystemObject")

While objFSO.FileExists(strFileName & strExtension) = True
      strFileName = Left(strFileName, Len(strFileName) - 3) & Right("000" & Int(Right(strFileName, 3)) + 1, 3)
Wend

Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c netstat 1 > " & strFileName & strExtension
Set objExec = objShell.Exec(strCommand)

' objShell.Run strCommand, 1, False

' Open browser to a specific site
' Set up an objIE object to open a browser to a specific site

WScript.Sleep 2000

strURL = "www.google.ca/"
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
        .left=50
        .top=50
        .height=600
        .width=1000
        .menubar=0
        .toolbar=1  
        .statusBar=0
        .navigate(strURL)
        .visible=1
End With

' Wait a while until IE has finished loading
Do While objIE.busy
      WScript.Sleep 10000
Loop

WScript.Sleep 30000

' Close the browser
objIE.Quit
Set objIE = Nothing

WScript.Sleep 5000

' Terminate the capture
objExec.Terminate
Set objExec = Nothing

' Clean up
Set objShell = Nothing
Set objFSO = Nothing

iter = iter + 1
Loop

WScript.Quit
=======================================

You may be willing to question the validity of some of my sleeps but the page that I am testing this against is a real pig. It turns out that I do need more sleeping because while IE thinks it is done at a certain point, there is still stuff happening (An Oracle page loading) that can not finish if the background if IE is killed. Then I have it looping and like a few seconds for the browser to shut down before reloading.

Thank you.
Tyrell M.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well it is getting damn close. I changed the IE window back to being visible, just like to know where it is at in the process. Toned down some of the sleeps. The other big change I made was instead of using taskkill by PID, I changed it to kill by process name. The former wasnt working but the change seemed to help.

I greatly appreciate all your help with this. I will fiddle with the last little bit myself. I am awarding you your points. You have for sure earned them! Thanks!

Tyrell M.
Thanks Tyrell.....very strange the process ID didn't work though....the task name may not actually catch the correct process.....as there may be multiple windows running......change this line:
intProcessID = objShell.Run(strCommand, 0, False)

to this
intProcessID = objShell.Run(strCommand, 1, False)

just to "see" the command prompt.....with no output because of the file output redirection...

Regards,

Rob.