In VB Script (VBS) generating URL using VARIABLE value and launching in DEFAULT BROWSER, (The VBS called from Batch)

Posted on 2017-12-07
Low Priority
Last Modified: 2017-12-07
I have a batch script MyBatch.bat . It successfully calls MyVbScript.vbs by the statement -

cscript //nologo %~dp0\MyVbScript.vbs %NewPort% where NewPort is a environment variable value.
MyVbScript.vbs is getting called successfully(tested) and value of NewPort is passed to VBS successfully(tested) from Batch script.
But I am NOT able to generate the URL for launching in default Browser. I want to generate the URL like -
http://localhost:7006/MyWebApplication where 7006 is value of NewPort variable.

 MyVbScript.vbs contains --

Option Explicit
Dim wsh
Dim port
Dim myNum
Set wsh=WScript.CreateObject("WScript.Shell")
port = WScript.Arguments(0)
'MsgBox port//For testing
wsh.Run " "http://localhost:"&port&"/MyWebApplication" "       ' This is not working

How to generate the URL and launch it in default browser( I dont want to specify crome or mozilla and Ie because use may not have a particular browser)
Question by:Aryabhatta M

Author Comment

by:Aryabhatta M
ID: 42395290
wsh.Run "http://localhost:"&port&"/MyWebApplication"  worked. Any seasoned programmer can suggest other code too if I have written more LOC unnecessarily.
LVL 15

Expert Comment

by:Máté Farkas
ID: 42395345
Put your expression first into a variable to be able to print what you want to execute:
URL = "http://localhost:" & port & "/MyWebApplication"
WScript.Echo URL
wsh.Run URL

Open in new window

LVL 61

Accepted Solution

Bill Prew earned 1000 total points
ID: 42395375
If you want to pass the port as a parm then the code you have is reasonable.

You could of course pass the whole URL as a parm to the VBS, but not sure if that fits your usage any better of not.

In addition, if you wanted you could pull the environment variable value in VBS rather than pass it as a parm, but if it were me I would probably stick with passing it as a parm.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 37

Expert Comment

ID: 42395446
Well, first of all: Is this the entire VBS code? Cause then I don't understand it.. why not just doing it in your batch directly using Start url?

And last but not least it is something like

Option Explicit

Dim port
Dim url
Dim wsh

port = WScript.Arguments(0)
url = "www.localhost:" & port & "/MyWebApplication" 
WScript.Echo port & "; " & url

Set wsh=WScript.CreateObject("WScript.Shell")
wsh.Run  url
Set wsh = Nothing

Open in new window

and run this while development and testing under a command line and invoke it via cscript.exe.

Author Comment

by:Aryabhatta M
ID: 42395641
No there were more in the VBS thats why vbs was needed, particularly for refreshing browser.

Author Closing Comment

by:Aryabhatta M
ID: 42395647

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

586 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