tags:

views:

120

answers:

3
+1  A: 

What are you trying to achieve by this script?

If you're doing load testing, there are plenty useful tools out there... Take a look at this list for example.

If you must do it in a batch file, what is your environment? Windows, I assume? Can you run Powershell or Windows Scripting Host files? Using these, you can fairly easily script your browser to open a website, delay for x seconds, and open another website.

There is some code here to open a website using WSH:

http://www.vistax64.com/vb-script/258346-how-script-ie-open-certain-webpage-prompted-user-pass.html

Code from above website:

Dim aLinks(2)
aLinks(0) = "http://localhost"
aLinks(1) = "http://otherhost"

Set oIE = CreateObject("InternetExplorer.Application") ', "oIE_")
oIE.Visible = True
oIE.FullScreen = False

'open a new window
oIE.Navigate2 aLinks(0)
Do While oIE.Busy
  WScript.Sleep 50
Loop

WScript.Sleep 2000
For J = Lbound(aLinks) To Ubound(aLinks)
  oIE.Navigate2 aLinks(J), navOpenInBackgroundTab 'navOpenInNewTab '
  Do While oIE.Busy
    WScript.Sleep 50
  Loop
  WScript.Sleep 3000
Next

WshShell.SendKeys "^{TAB}" 'go to the 2nd tab
WScript.Sleep 100
on error goto 0
Sune Rievers
+1  A: 
  • For opening a Web site, you can call IExplore.exe (or whatever browser is available) with the URL in the command line, and it will.

  • The trickier part is inserting a 10s delay without using specialized utilities. Believe it or not, Microsoft's recommended procedure for this is to ping a non-existent site with a timeout (given by option -w) of 10 seconds.

Example:

@ECHO OFF
"C:\Program Files\Internet Explorer\Iexplore.exe" http://my.site/1.html
ping -n1 -w10 10.9.8.7
"C:\Program Files\Internet Explorer\Iexplore.exe" http://my.site/2.html
ping -n1 -w10 10.9.8.7
"C:\Program Files\Internet Explorer\Iexplore.exe" http://my.site/3.html
ping -n1 -w10 10.9.8.7
"C:\Program Files\Internet Explorer\Iexplore.exe" http://my.site/4.html
ping -n1 -w10 10.9.8.7
Carl Smotricz
What, that's crazy... but a bit funny at the same time :) I'm talking about the delay by ping here...
Sune Rievers
+1 A bit hardcoded and difficult to maintain, but answers the question perfectly :)
Sune Rievers
Oh wait, shouldn't the browsers close afterwards? If you have 1000 websites to test, you should make sure that you have set IE to reuse existing windows, or you'll have a shitstorm of IE instances running...
Sune Rievers
If this were testing, I think this wouldn't be a batch file. You can't test whether the site exists anyway when just opening a browser.
Joey
Maybe it's for load testing? So the url list consists of multiple urls for the same site... otherwise I can't really think of a legitimate use of such a script?
Sune Rievers
Hey, wait a minute. Shouldn't we be providing the answers, not the questions?
Carl Smotricz
Well, "What the hell are you trying with that?" isn't an uncommon question around here :-)
Joey
+1  A: 

As others have said, opening a browser can work for opening a web page. Rather than hard-coding the browser, you can simply use start:

start "" "http://my/url"

To wait 10 seconds you can also ping yourself 11 times:

ping localhost -n 11 >nul 2>&1

or, starting with Windows Vista you can use timeout:

timeout /T 10 /NOBREAK >nul

So you can cobble together something like this:

setlocal enabledelayedexpansion enableextensions

set TIMEOUT=10

set WEBPAGE0=http://google.com
set WEBPAGE1=http://stackoverflow.com
set WEBPAGE2=http://en.wikipedia.org
set WEBPAGE3=...  extend as needed

set N=0
set /a X=TIMEOUT+1
:loop
if not defined WEBPAGE%N% goto :eof
start "" "!WEBPAGE%N%!"
ping localhost -n %X% >nul 2>&1
goto loop
Joey
+1 Nice solution with timeout, seems so much better than pinging :)
Sune Rievers
So it only took Microsoft 25 years to develop the equivalent of the `sleep` command? Can't wait to see what the next 25 years will bring!
Carl Smotricz
Oh, you always had one with `ping` :-). Also remember that Windows is not UNIX and not necessarily the same rules need to apply. There is simply no need to have a command-line wrapper around every single API function. At least for 99 % of users. Besides, more powerful capabilities are present in both WSH and PowerShell, the latter of which I find not only on par but actually better than UNIX-like command-line environments.
Joey
Heh, I'm more than 2x your age and remember when the Norton Utilities included 4DOS and other programs to help make a batch environment useful: Waiting, single-char input, text formatting, various conversions... I agree that PowerShell is good stuff but it was an awfully long time coming.
Carl Smotricz
Ah well, I never really liked 4DOS and 4NT somehow. But back when I tried them I wasn't much of a command-line user. But I *do* remember the Batch Enhancer (BE), and yes, it was handy :-). But I actually do like `cmd`, somehow. It's relaxing to try to solve problems in such a language. Maybe I'm just weird :-)
Joey