Batch file - how to launch non-default browser? - windows

When I run this:
"C:\Program Files (x86)\Internet Explorer\iexplore.exe" http://target_web_site?param=arg
from the command line, the target page is opened in IE (as expected).
But when I attempt to do this in a batch file:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:; Check the input
IF "%1"=="" GOTO NoArg
SET url=http://target_web_site?param=
SET url=%url%%1
start "C:\Program Files (x86)\Internet Explorer\iexplore.exe" !url!
GOTO AllDone
:NoArg
echo USAGE:
echo %0 Arg
:AllDone
the target page is opened in my default browser (Chrome). I'd like to understand why this happens and how to control which browser launches? When IE is the default browser, the target opens in IE. Thanks.

The first set of quotes in start (which is the wrong command anyway) is the Windows Title. So you are executing a web site. There is no need for delayed expansion.
"C:\Program Files (x86)\Internet Explorer\iexplore.exe" %url%

I read your questions, and I think you want to start a page from CMD (batch).
I'm doing this with the start command.
You can open a page with the command:
start "http://stackoverflow.com/questions/38776220/batch-file-how-to-launch-non-default-browser"
(Change the page)
Hope I helped you!

Related

Getting a Windows batch file to execute an application in background and then execute the next statement

I am having issue trying to get my Windows batch file to launch the Jetty web server in the background and then launch IE. The current behavior is that after it started my Jetty web server, it doesn't return to launch IE. It simply stuck there until I terminate the web server and then batch script will then proceed and launch IE.
Here's my batch script
SET JAVA_HOME=".\openjdk-1.8.0.141"
SET JETTY_HOME=".\jetty-distribution-9.4.6.v20170531"
start /B cd /d "%~dp0" & %JAVA_HOME%\bin\javaw -DSTOP.PORT=8081 -DSTOP.KEY=stop_jetty -Djetty.base=%JETTY_HOME% -jar %JETTY_HOME%\start.jar
"C:\Program Files\Internet Explorer\iexplore.exe" http://localhost:8080/foo-tools
Can you spot anything obvious here? I already used the 'start /B' to attempt to launch it in the background. I have to change directory back to the current working directory, otherwise the variable that I set will not work.
Thanks in advance!
Why not simplify things and stipulate the script path with the START's /D <Path> parameter?
SET "JAVA_HOME=openjdk-1.8.0.141"
SET "JETTY_HOME=jetty-distribution-9.4.6.v20170531"
START "" /D "%~dp0" "%JAVA_HOME%\bin\javaw" -DSTOP.PORT=8081 -DSTOP.KEY=stop_jetty -Djetty.base="%JETTY_HOME%" -jar "%JETTY_HOME%\start.jar"
START "" "%PROGRAMFILES%\Internet Explorer\iexplore.exe" http://localhost:8080/foo-tools
I missed off START's /B parameter because it was my understanding that javaw.exe doesn't open a CMD window anyhow. If my understanding is incorrect then please add it back just before "%JAVA_HOME%.
The START command for IE is only really necessary if you need the script again or don't want the cmd window to remain open.

startup batch file hangs up on second command

I created a startup bat file that looks like this
taskkill /im RemoteDesktopManager.exe
C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
"C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
"C:\Program Files\Mozilla Firefox\firefox.exe"
it does the first and second, but won't go any further, they all exist
how can I fix this?
update : I tried suggestion provided by #phd443322 and wrote this:
taskkill /im RemoteDesktopManager.exe
start "" /w C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" /w "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" /w "C:\Program Files\Mozilla Firefox\firefox.exe"
intrestingly each command still waits for that program to be closed to continue to the next.
so why still not working?
Below there is a working Batch file, as first advised by phd443322:
taskkill /im RemoteDesktopManager.exe
start "" C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"
Batch files wait for programs to exit unlike interactive. These are the rules documented in the Start command.
If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:
non-executable files may be invoked through their file association just
by typing the name of the file as a command. (e.g. WORD.DOC would
launch the application associated with the .DOC file extension).
See the ASSOC and FTYPE commands for how to create these
associations from within a command script.
When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt. This new behavior does NOT occur if executing
within a command script.
When executing a command line whose first token is the string "CMD "
without an extension or path qualifier, then "CMD" is replaced with
the value of the COMSPEC variable. This prevents picking up CMD.EXE
from the current directory.
When executing a command line whose first token does NOT contain an
extension, then CMD.EXE uses the value of the PATHEXT
environment variable to determine which extensions to look for
and in what order. The default value for the PATHEXT variable
is:
.COM;.EXE;.BAT;.CMD
Notice the syntax is the same as the PATH variable, with
semicolons separating the different elements.
When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name. If it does, the
START command launches the Explorer on that path. If done from the
command line, it is the equivalent to doing a CD /D to that path.

How to use the start command in a batch file?

I have a batch file that starts an app with a lot of command-line parameters:
"C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\WebDev.WebServer40.exe" /port:1672 /path:"C:\Code.Net\My App\Iteration 6\REL_6.8.806_PerfEnhanceV\Fusion\Code\CC.Fusion\CC.Fusion.Services" /vpath:"/FusionServices"
The problem is that when I run the batch file, the DOS window stays up until the command completes and I would like it to go away. So I tried using the start command, but placing it in front, like this:
start "C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\WebDev.WebServer40.exe" /port:1672 /path:"C:\Code.Net\My App\Iteration 6\REL_6.8.806_PerfEnhanceV\Fusion\Code\CC.Fusion\CC.Fusion.Services" /vpath:"/FusionServices"
But I get an error stating that Invalid switch - "/port:1672"
I have also tried escaping the double quotes, but I was not successful.
How do I fix that?
An extra pair of rabbits' ears should do the trick.
start "" "C:\Program...
START regards the first quoted parameter as the window-title, unless it's the only parameter - and any switches up until the executable name are regarded as START switches.
I think this other Stack Overflow answer would solve your problem: How do I run a bat file in the background from another bat file?
Basically, you use the /B and /C options:
START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1

Running Several batch Commands in Sequence

When i run this batch file command as a single batch file the second command does not run.However when i run them as individual batch file commands they work fine.
"C:\Program Files\Mozilla Firefox\firefox.exe" -P "america" -no-remote http://hakikahost.com/error.html
"nircmd.exe" win hide process "firefox.exe"
tried creating 1 single batch file which called with a call the two batch files now having separated the batch files command separately like this
call test.bat
call hide.bat
where test.bat contained the first command and hide.bat contained the second command but it still didnt work.What am i doing wrong?
It may be that firefox.exe never returns until you close the window. Try using start to launch the applications, as start will return as soon as the application has launched.
start "" "C:\Program Files\Mozilla Firefox\firefox.exe" -P "america" -no-remote http://hakikahost.com/error.html
start "" "nircmd.exe" win hide process "firefox.exe"
The first command, "C:\Program Files\Mozilla Firefox\firefox.exe" does not return until the Fx session has ended (ie you EXIT from it)
Then there are no Fx executables so the second command can't hide the process that doesn't exist.
Try
START "" "C:\Program Files\Mozilla Firefox\firefox.exe" -P "america" -no-remote http://hakikahost.com error.html
"nircmd.exe" win hide process "firefox.exe"
The only difference is the START "" before the firefox-invocation. Note that the empty-quoted-string is required - you could enter a string between the quotes if you like - this becomes the window title.

I want to open a link in several browsers simultaneously for testing

I want to open for example "localhost/MySite" in IE9, Chrome, Firefox, Opera, and Safari simultaneously for QA testing.
I was thinking a batch file of some sort would be an effective way of doing this.
i.e. double click "test.bat" and a command line comes up with a query "Website to test: ". I enter in "localhost/MySite", and the batch file opens all of these browsers and goes to that site.
I'm not sure if this is possible, as I've not found anything on the topic, but it would be very useful for me! Always good to save a bit of time. :)
Thanks!
Try
setlocal
set /p url=Website to test:
start chrome %url%
start firefox %url%
start iexplore %url%
start opera %url%
start safari %url%
you can just do something like this in a batch script:
cd "/path/to/file1"
start program1.exe
cd "/path/to/file2"
start program2.exe
This ended up working great:
set /p url= [Desired URL to test?]
if exist "%ProgramFiles(x86)%\Internet Explorer\iexplore.exe" start "" "%ProgramFiles(x86)%\Internet Explorer\iexplore.exe" %url%&
if exist "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" start "" "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" %url%&
if exist "%ProgramFiles(x86)%\Opera\opera.exe" start "" "%ProgramFiles(x86)%\Opera\opera.exe" %url%&
if exist "%ProgramFiles(x86)%\Safari\Safari.exe" start "" "%ProgramFiles(x86)%\Safari\Safari.exe" %url%&
if exist "%USERPROFILE%\AppData\Local\Google\Chrome\Application\chrome.exe" start "" "%USERPROFILE%\AppData\Local\Google\Chrome\Application\chrome.exe" %url%&

Resources