Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a batch file 1

Status
Not open for further replies.

NetNodeMan

Technical User
Mar 30, 2005
65
IE
Hello folks,
I have the following VB code and as those of you in the know can see wht it does is that it goes to a specific web page. Within this webpage there are two text boxes. It enters data in the two text boxes and clicks on "Continue". A page appears in internet explorer telling me I'm a good boy (or words to that effect)
Now I'm completely useless at this so bear with me. I want to add code to this script so that if I get a "This page cannot be displayed message" i.e. the process doesn't work, I get a popup and a sound played.
Ideally I would like to call a batch file for this i.e. send an email when it doesn't work.

I would like help with both possibilities please and please remember I'm new so be gentle.
Thanks in advance



Option explicit
Dim IE, sLoop, sLoop2, isOperationComplete, dState, sList, sError
Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
End Sub
Sub objIE_NavigateComplete2(pDisp,URL)
isOperationComplete = true
End Sub
Function Wait()
sError = 1
Do while IE.busy
wscript.sleep 1000
sError = sError + 1
Loop
if sError > 20 then
exit Function
end if
dState = IE.document.readystate
while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
End Function

sLoop = "forever"
dState = ""
Do while sLoop = "forever"
sLoop2 = "forever"
Do while sLoop2 = "forever"
'Commented out by Finner.
'************ HTTP Check
'set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
'IE.navigate("WEB PAGE ADDRESS GOES IN HERE")
'IE.visible = True
'wait()
'if sError > 20 then
' wscript.echo now() & " Http Error is there an error code for this date and time ?"
' ie.quit
' exit do
'end if
'IE.quit
'************ End of HTTP check
'///Finner - no need for this I think
'************ HTTPS Check
set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
IE.Navigate("")
IE.visible = True
wait()
if sError > 20 then
wscript.echo now() & " Https Page 1 Error is there an error code for this date and time ?"
ie.quit
exit do
end if '/////no need for this I think
IE.document.getElementByID("VRM").value = "LD02YXZ" 'this is the VRM field
IE.document.getElementByID("TestNumber").value = "152008305118" 'this is the Test Number field
IE.document.getElementByID("cmdSubmit").click
wait()
if sError > 20 then
wscript.echo now() & " Https Page 2 Error is there an error code for this date and time ?"
ie.quit
exit do
end if
IE.quit
sLoop2 = "done"
Loop
'Wait for 5 Minutes
Wscript.Sleep 30000
 
Ok. I have sorted this so that a message comes up if an error occurs. However, the message only comes up in the command prompt and the popup window isn't visible on the main screen as an active window (it appears minimised).

What I'm really looking to do now is that when the test fails I call a batch file (for a sound) and a popup appears. When the test runs ok, the text that appears in the command sheel is "Test Ran ok". Is it also possible to export this to a log file?
 
[tt]>Do while sLoop = "forever"
>Do while sLoop2 = "forever" [/tt]
It seems there is only one Loop down there.

>the message only comes up in the command prompt
Use msgbox instead of wscript.echo.

>the popup window isn't visible on the main screen as an active window (it appears minimised).
Is it using the .popup() method? If yes, you cannot control the focus. It is in the hand of os.

>when the test fails I call a batch file (for a sound)
Before every exit do, add a line to run the batch
[tt] createobject("wscript.shell").run "c:\path\xyz.bat"[/tt]
(with some refinement if necessary.)

>...a popup appears
Add also
[tt] msgbox "your message"[/tt]

>When the test runs ok, the text that appears in the command sheel is "Test Ran ok".
Before each loop statement, add a message box
[tt] msgbox "Test ran ok."[/tt]
 
Option explicit

Dim IE, sLoop, sLoop2, isOperationComplete, dState, sList, sError

Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
End Sub

Sub objIE_NavigateComplete2(pDisp,URL)
isOperationComplete = true
End Sub

Function Wait()
sError = 1
Do while IE.busy
wscript.sleep 1000
sError = sError + 1
Loop
if sError > 20 then
exit Function
end if
dState = IE.document.readystate
while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
End Function


sLoop = "forever"

dState = ""
Do while sLoop = "forever"
sLoop2 = "forever"
Do while sLoop2 = "forever"


'************ HTTPS Check

set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
IE.Navigate(" IE.visible = True
wait()


'Added by Finner, 8 Jul 2005. Populates the correct fields.
IE.document.getElementByID("VRM").value = "LD02YXZ" 'this is the VRM field
'IE.document.getElementByID("V5RefNo").value = "1" 'this is the V5 field
IE.document.getElementByID("TestNumber").value = "152008305118" 'this is the Test Number field

IE.document.getElementByID("cmdSubmit").click

wait()

if sError < 20 then
wscript.echo now() & " Vosa Test OK"
createobject("wscript.shell").run "c:\ringin.bat"
MsgBox " Test OK"
ie.quit
exit do
end if

'else
'if sError > 20 then
' wscript.echo now() & " Test FAILURE"
' MsgBox " Test Failure"
' ie.quit
' exit do
' end if '/////no need for this I think

'' 'vosatestlog.WriteLine "Test OK"
'Edited out on 19th July
'MsgBox " VOSA Test Failure"
'wscript.echo now() & " Test FAILURE"
'MsgBox.visible = True
' ie.quit
' exit do
'' end if
'' IE.quit
'************ End of HTTPS Check

sLoop2 = "done"
Loop
'Wait for 5 Minutes
Wscript.Sleep 60000
Loop


Thanks a million. Here's a quick update. It now runs the test and gives a popup when the test runs ok. It also runs the batchfile (plays a sound) when the test runs ok. I have 3 questions:

1) How do I get the text box that appears to disappear without clicking OK myself?
2) How do I capture text to send to a log file?
3) Can I simply put in an else statement between the two ifs? (the serror <20 and if serror > 20). Apologies again but VB and programming certainly aren't my field.
 
Thanks for the feedback.

A slight issue of sError. In the function wait(), when you loop the ie.busy, and it successfully complete the wait and ie is no longer busy, you should be ready to proceed further even after waiting for quite a long while. Hence, it seems unreasonable to abandon at that stage? If sError is <20 at that stage, you nevertheless need to change
[tt] while (dState <> "complete" and sError < 20)[/tt]
to
[tt] while (dState <> "complete" and sError <[red]=[/red]20)[/tt]
otherwise it would ever fail with sError>20. It would fail at sError=20!

Upon your further questions
[1] You can use popup method of wscript.shell to replace the msgbox.
[tt] createobject("wscript.shell").popup "The message.",15[/tt]
(where 15 is the time to wait in unit seconds.)
[2] What log file?
[3] Sure the if statement's structure is
[tt] if ... then
'etc etc
else
'etc etc
end if[/tt]
But if you have 2 if ... end if with conditional sError>20 and sError<[red]=[/red]20, then you covered all the outcome. (Look again at my notes above.) And effectively the same.


 
Thanks tsuji. I am a complete newbie at this and unfortunately don't have time to study VB before I get time to get this script out & ready.

Anyway, what happens is this:

A batch file runs opening a command shell as shown below. It then opens up an IE page and does the test. Then I have a popup, an alarm and confirmation (with date and time) in the command shell that the test ran correctly.

Now, what I basically want to do is the following:

1) I want to export the text shown in the command shell (inc date and time) to a text file and I want the text file to append each time the test runs.

2) I want the alarm to run but I also want Windows Media Player to close after the alarm has run.

3) I want a popup but when I tried the popup code it also closed the command shell. I want the command shell to remain open as I want the code to loop forever.

Any help much appreciated.

 
Very uncertain. A re-list with logfile thing etc see how it goes. It is supposed to be hosted by cscript if I understand your description correctly.
[tt]
Option explicit

Dim IE, sLoop, sLoop2, isOperationComplete, dState, sList, sError

Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
logfile.writeline now() & " Error " & StatusCode & " accessing " & URL
End Sub

Sub objIE_NavigateComplete2(pDisp,URL)
isOperationComplete = true
End Sub

Function Wait()
sError = 1
Do while IE.busy
wscript.sleep 1000
sError = sError + 1
Loop
if sError > 20 then
exit Function
end if
dState = IE.document.readystate
while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
End Function

dim wshshell, fso, logfile, slog
slog="c:\log\logfilename.txt" 'your input
set wshshell=createobject("wscript.shell")
set fso=createobject("scripting.filesystemobject")

sLoop = "forever"

dState = ""
Do while sLoop = "forever"
sLoop2 = "forever"
Do while sLoop2 = "forever"

set logfile=fso.opentextfile(slog,8,true)

'************ HTTPS Check

set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
IE.Navigate(" IE.visible = True
wait()


'Added by Finner, 8 Jul 2005. Populates the correct fields.
IE.document.getElementByID("VRM").value = "LD02YXZ" 'this is the VRM field
'IE.document.getElementByID("V5RefNo").value = "1" 'this is the V5 field
IE.document.getElementByID("TestNumber").value = "152008305118" 'this is the Test Number field

IE.document.getElementByID("cmdSubmit").click

wait()

if sError < 20 then
wscript.echo now() & " Vosa Test OK"
logfile.writeline now() & " Vosa Test OK"
wshshell.run "c:\ringin.bat"
wshshell.popup " Test OK",15
logfile.close
ie.quit
exit do
'else 'sError>=20, actually =20
' wscript.echo now() & " Test FAILURE"
' logfile.writelin now() & " Test FAILURE"
' wshshell.popup " Test Failure"
' logfile.close
' ie.quit
' exit do
end if '/////no need for this I think

'This section is uncertain.
'vosatestlog.WriteLine "Test OK"
'Edited out on 19th July
'MsgBox " VOSA Test Failure"
'wscript.echo now() & " Test FAILURE"
'MsgBox.visible = True 'this is not vbscript, delete it
' ie.quit
' exit do
'' end if
'' IE.quit
'************ End of HTTPS Check

sLoop2 = "done"
Loop
'Wait for 5 Minutes
Wscript.Sleep 60000
Loop
set ie=nothing
set logfile=nothing
set fso=nothing
set wshshell=nothing
[/tt]
 
Option explicit
Dim IE, sLoop, sLoop2, isOperationComplete, dState, sList, sError
Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
End Sub
Sub objIE_NavigateComplete2(pDisp,URL)
isOperationComplete = true
End Sub
Function Wait()
sError = 1
Do while IE.busy
wscript.sleep 1000
sError = sError + 1
Loop
if sError > 20 then
exit Function
end if
dState = IE.document.readystate
while (dState <> "complete" and sError <= 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
End Function

sLoop = "forever"
dState = ""
Do while sLoop = "forever"
sLoop2 = "forever"
Do while sLoop2 = "forever"


'************ HTTPS Check
set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
IE.Navigate(IE.visible = True
wait()

'Added by Finner, 8 Jul 2005. Populates the correct fields.
IE.document.getElementByID("VRM").value = "LD02YXZ" 'this is the VRM field
'IE.document.getElementByID("V5RefNo").value = "1" 'this is the V5 field
IE.document.getElementByID("TestNumber").value = "152008305118" 'this is the Test Number field
IE.document.getElementByID("cmdSubmit").click
wait()


if sError < 20 then
wscript.echo now() & " Test OK"
'createobject("wscript.shell").run "c:\ringin.bat"
'This is calling the alarm but WMP does not close after alarm runs

IE.quit
'dim pop
'set pop=createobject("wscript.shell")
'p=pop.popup("VOSA Test OK",2) I did have a popup box with a timeout on it but the timeout also closed the command prompt which I dont want. Any help?

MsgBox " Test OK" 'Now I have to click OK but don't want to. Can a msgbox be timed out?
exit do

end if
'else
'if sError >= 20 then
' wscript.echo now() & (" Test FAILURE",5)
' createobject("wscript.shell").run "c:\ringin.bat"
' MsgBox " Test Failure"
' IE.quit
' exit do
' end if '/////no need for this I think

'************ End of HTTPS Check
sLoop2 = "done"
Loop
'Wait for 5 Minutes
Wscript.Sleep 300000
Loop
 
tsuji,

Your input has been invaluable. Thanks a million. Now my code runs almost as I want it and I can write to the logfile. I still have one or two problems however:

1) The if/else simply isn't working. Have you an idea what may be wrong with it?
if sError < 20 then
wscript.echo now() & " Vosa Test OK"
logfile.writeline now() & " Vosa Test OK"
wshshell.run "c:\ringin.bat"
wshshell.popup " VOSA Test OK",3
logfile.close
ie.quit
exit do
else sError>=20, actually =20
wscript.echo now() & " Test FAILURE"
logfile.writelin now() & " Test FAILURE"
wshshell.popup " Test Failure"
logfile.close
ie.quit
exit do
end if

2: Once the message box saying "Test OK" disappears after the timeout, the command prompt closes. I want the command prompt to remain open if possible as this test needs to run every ten minutes.

Any more ideas? Again, thanks a million for your help to date.
 
[tt]>else sError>=20, actually =20[/tt]
actually the last part is doubly commented in the original and is intended to be a genuine comment.
[tt]else [COLOR=red yellow]'[/color]sError>=20, actually =20[/tt]

Further, I see you now change <20 to <=20 up there which I remarked upon the necessity. Then, after in my re-list, I respect your original intention and I kept <20 that way. So, if you eventially adapt <=20, then go through the related locations see if things are okay. Maybe you need then to change sError<=20! That's a bit of a confusion.

 
Thanks for all your help tsuji.

Finally, if the webpage is unavailable can I also put an error message in there and how do I do this?
 
I would say put something here using my listing.
[tt]
while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
if dState<>"complete" then 'sError is 20 here
logfile.writeline now() & " - " & dState & " - " & "Document failed to completely loaded for unknown reason."
end if
end function
[/tt]
I use my listing because I no longer see log file related lines in your update!

 
Thanks tjusi,

Below is the updated version of my code. Now everything works but I still need to popup an error message if the internet web page does not appear. I tried what you already gave me but this does not work. Any ideas? You have been brilliant. Thanks.


Option explicit

Dim IE, sLoop, sLoop2, isOperationComplete, dState, sList, sError

Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
End Sub

Sub objIE_NavigateComplete2(pDisp,URL)
isOperationComplete = true
End Sub

Function Wait()
sError = 1
Do while IE.busy
wscript.sleep 1000
sError = sError + 1
Loop
if sError > 20 then
exit Function
end if
dState = IE.document.readystate
'while (dState <> "complete" and sError < 20)
' Wscript.sleep 1000
' dState = IE.document.readystate
' sError = sError + 1

while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend

end function


dim wshshell, fso, logfile, slog
slog="c:\log\logfilename.txt" 'your input
set wshshell=createobject("wscript.shell")
set fso=createobject("scripting.filesystemobject")

sLoop = "forever"

dState = ""
Do while sLoop = "forever"
sLoop2 = "forever"
Do while sLoop2 = "forever"
set logfile=fso.opentextfile(slog,8,true)


'************ HTTPS Check

set IE=Wscript.CreateObject("InternetExplorer.Application","objIE_")
IE.Navigate(" IE.visible = True
wait()



'Added by Finner, 8 Jul 2005. Populates the correct fields.
IE.document.getElementByID("VRM").value = "LD02YXZ" 'this is the VRM field
'IE.document.getElementByID("V5RefNo").value = "1" 'this is the V5 field
IE.document.getElementByID("TestNumber").value = "152008305118" 'this is the Test Number field

IE.document.getElementByID("cmdSubmit").click

wait()

if sError <= 20 then
wscript.echo now() & " Test OK"
logfile.writeline now() & " Test OK"
logfile.close
ie.quit
exit do
else
wscript.echo now() & " Test FAILURE"
logfile.writeline now() & " Test FAILURE"
wshshell.run "c:\ringin.bat"
wshshell.popup " Web Check has Failed. Please Investigate"
logfile.close
ie.quit
exit do
end if


'************ End of HTTPS Check

sLoop2 = "done"
Loop
'Wait for 5 Minutes
Wscript.Sleep 600000
Loop
 
[tt]
while (dState <> "complete" and sError < 20)
Wscript.sleep 1000
dState = IE.document.readystate
sError = sError + 1
wend
[green]if dState<>"complete" then 'sError is 20 here
logfile.writeline now() & " - " & dState & " - " & "Document failed to completely loaded for unknown reason."
wshshell.popup "Document failed to completely loaded for unknown reason."
end if[/green]
end function
[/tt]
 
Correction:
[tt]
wshshell.popup "Document failed to completely loaded for unknown reason."[red],15[/red]
[/tt]
Also I notice that all your popups lacks the second parameter with is in unit of seconds. (see my re-list)
 
Yes tjusi,

That does not appear to work. I simply get the following in my command prompt when the script runs:

Error 404 accessing Object required: 'document.getElementByID(...)

Also, I wish the popups to stay always when an error occurs so this is ok.
 
To check navigation error,

[1] Make a global boolean variable, ieErr say. Initialise it to false.
[tt]
dim ieErr : ieErr=false 'global scope
[/tt]
[2] In the callback, add a new line.
[tt]
Sub objIE_NavigateError(pDisp,URL,TargetFrameName,StatusCode,Cancel)
wscript.echo now() & " Error " & StatusCode & " accessing " & URL
ieErr=true
End Sub
[/tt]
[3] At the place above, instead of checking document.readystate, do this instead.
[tt]
if ieErr then 'navigation error
logfile.writeline now() & " - " & dState & " - " & "Document failed to completely loaded for unknown reason."
wshshell.popup "Document failed to completely loaded for unknown reason."
end if
end function
[/tt]
 
Amendment

[3'] At the place above, instead of checking document.readystate, do this instead.
[tt]
if ieErr then 'navigation error
logfile.writeline now() & " - " & dState & " - " & "Document failed to completely loaded for unknown reason."
wshshell.popup "Document failed to completely loaded for unknown reason."[red],15[/red]
[blue]ieErr=false[/blue] 'reset it
end if
end function
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top