?
Solved

Involved circular VBScript

Posted on 2007-10-01
9
Medium Priority
?
491 Views
Last Modified: 2013-11-26
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!
0
Comment
Question by:Nechako91
  • 5
  • 4
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 19995776
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.
0
 

Author Comment

by:Nechako91
ID: 20001667
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!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20002148
Great, no worries.  Take you time.  Let me know if you need anything else...like a loop through multiple sites or something....

Regards,

Rob.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Nechako91
ID: 20010443
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!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20010563
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.
0
 

Author Comment

by:Nechako91
ID: 20010717
'======================================================================
' 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.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 20011883
OK, try this version...I have used the objShell.Run instead of objShell.Exec so that we get the process ID, and continue the script.....then at the end it terminates that process ID with the Windows XP command TaskKill

I'm still not sure if you'll need that many sleeps, but see how you go....

'======================================================================
' 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
intProcessID = objShell.Run(strCommand, 0, 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=0
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
strCommand = "%comspec% /c taskkill /PID " & intProcessID & " /F"
objShell.Run strCommand, 0, True

' Clean up
Set objShell = Nothing
Set objFSO = Nothing

iter = iter + 1
Loop

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

Regards,

Rob.
0
 

Author Comment

by:Nechako91
ID: 20015311
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20018619
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.
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.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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