简体   繁体   English

如何从批处理文件启动多个 Internet Explorer 窗口/选项卡?

[英]How to launch multiple Internet Explorer windows/tabs from batch file?

I would like a batch file to launch two separate programs then have the command line window close.我想要一个批处理文件来启动两个单独的程序,然后关闭命令行窗口。 Actually, to clarify, I am launching Internet Explorer with two different URLs.实际上,澄清一下,我正在使用两个不同的 URL 启动 Internet Explorer。

So far I have something like this:到目前为止,我有这样的事情:

start "~\iexplore.exe" "url1"
start "~\iexplore.exe" "url2"

What I get is one instance of Internet Explorer with only the second URL loaded.我得到的是一个只加载了第二个 URL 的 Internet Explorer 实例。 Seems the second is replacing the second.似乎第二个正在取代第二个。 I seem to remember a syntax where I would load a new command line window and pass the command to execute on load, but can't find the reference.我似乎记得一个语法,我将加载一个新的命令行窗口并传递命令以在加载时执行,但找不到引用。

As a second part of the question: what is a good reference URL to keep for the times you need to write a quick batch file?作为问题的第二部分:在您需要编写快速批处理文件的时候保留什么是好的参考 URL?

Edit: I have marked an answer, because it does work.编辑:我已经标记了一个答案,因为它确实有效。 I now have two windows open, one for each URL.我现在打开了两个窗口,每个 URL 一个窗口。 (thanks!) The funny thing is that without the /d approach using my original syntax I get different results based on whether I have a pre-existing Internet Explorer instance open. (谢谢!)有趣的是,如果没有使用我的原始语法的 /d 方法,根据我是否打开了预先存在的 Internet Explorer 实例,我会得到不同的结果。

  • If I do I get two new tabs added for my two URLs (sweet!)如果我这样做了,我会为我的两个 URL 添加两个新标签(太棒了!)
  • If not I get only one final tab for the second URL I passed in.如果不是,我只会得到我传入的第二个 URL 的最后一个选项卡。

Try this in your batch file:在您的批处理文件中试试这个:

@echo off
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE www.google.com
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE www.yahoo.com

You can use either of these two scripts to open the URLs in separate tabs in a (single) new IE window.您可以使用这两个脚本中的任何一个在(单个)新 IE 窗口中的单独选项卡中打开 URL。 You can call either of these scripts from within your batch script (or at the command prompt):您可以从批处理脚本中(或在命令提示符处)调用这些脚本中的任何一个:

JavaScript JavaScript
Create a file with a name like: "urls.js" :创建一个名为“urls.js”的文件

var navOpenInNewWindow = 0x1;
var navOpenInNewTab = 0x800;
var navOpenInBackgroundTab = 0x1000;

var intLoop = 0;
var intArrUBound = 0;
var navFlags = navOpenInBackgroundTab;
var arrstrUrl = new Array(3);
var objIE;

    intArrUBound = arrstrUrl.length;

    arrstrUrl[0] = "http://bing.com/";
    arrstrUrl[1] = "http://google.com/";
    arrstrUrl[2] = "http://msn.com/";
    arrstrUrl[3] = "http://yahoo.com/";

    objIE = new ActiveXObject("InternetExplorer.Application");
    objIE.Navigate2(arrstrUrl[0]);

    for (intLoop=1;intLoop<=intArrUBound;intLoop++) {

        objIE.Navigate2(arrstrUrl[intLoop], navFlags);

    }

    objIE.Visible = true;
    objIE = null;


VB Script VB 脚本
Create a file with a name like: "urls.vbs" :创建一个名为“urls.vbs”的文件

Option Explicit

Const navOpenInNewWindow = &h1
Const navOpenInNewTab = &h800
Const navOpenInBackgroundTab = &h1000

Dim intLoop       : intLoop = 0
Dim intArrUBound  : intArrUBound = 0
Dim navFlags      : navFlags = navOpenInBackgroundTab

Dim arrstrUrl(3)
Dim objIE

    intArrUBound = UBound(arrstrUrl)

    arrstrUrl(0) = "http://bing.com/"
    arrstrUrl(1) = "http://google.com/"
    arrstrUrl(2) = "http://msn.com/"
    arrstrUrl(3) = "http://yahoo.com/"

    set objIE = CreateObject("InternetExplorer.Application")
    objIE.Navigate2 arrstrUrl(0)

    For intLoop = 1 to intArrUBound

        objIE.Navigate2 arrstrUrl(intLoop), navFlags

    Next

    objIE.Visible = True
    set objIE = Nothing


Once you decide on "JavaScript" or "VB Script" , you have a few choices:一旦您决定使用"JavaScript""VB Script" ,您有几个选择:

If your URLs are static:如果您的网址是静态的:

1) You could write the "JS/VBS" script file (above) and then just call it from a batch script. 1)您可以编写“JS/VBS”脚本文件(如上),然后从批处理脚本中调用它。

From within the batch script (or command prompt), call the "JS/VBS" script like this:在批处理脚本(或命令提示符)中,像这样调用“JS/VBS”脚本:

cscript //nologo urls.vbs
cscript //nologo urls.js


If the URLs change infrequently:如果 URL 不经常更改:

2) You could have the batch script write the "JS/VBS" script on the fly and then call it. 2)您可以让批处理脚本即时编写“JS/VBS”脚本,然后调用它。


If the URLs could be different each time:如果 URL 每次都不同:

3) Use the "JS/VBS" scripts (below) and pass the URLs of the pages to open as command line arguments: 3) 使用“JS/VBS”脚本(如下)并传递要打开的页面的 URL 作为命令行参数:

JavaScript JavaScript
Create a file with a name like: "urls.js" :创建一个名为“urls.js”的文件

var navOpenInNewWindow = 0x1;
var navOpenInNewTab = 0x800;
var navOpenInBackgroundTab = 0x1000;

var intLoop = 0;
var navFlags = navOpenInBackgroundTab;
var objIE;
var intArgsLength = WScript.Arguments.Length;

    if (intArgsLength == 0) {

        WScript.Echo("Missing parameters");
        WScript.Quit(1);

    }

    objIE = new ActiveXObject("InternetExplorer.Application");
    objIE.Navigate2(WScript.Arguments(0));

    for (intLoop=1;intLoop<intArgsLength;intLoop++) {

        objIE.Navigate2(WScript.Arguments(intLoop), navFlags);

    }

    objIE.Visible = true;
    objIE = null;


VB Script VB 脚本
Create a file with a name like: "urls.vbs" :创建一个名为“urls.vbs”的文件

Option Explicit

Const navOpenInNewWindow = &h1
Const navOpenInNewTab = &h800
Const navOpenInBackgroundTab = &h1000

Dim intLoop
Dim navFlags      : navFlags = navOpenInBackgroundTab
Dim objIE

    If WScript.Arguments.Count = 0 Then

        WScript.Echo "Missing parameters"
        WScript.Quit(1)

    End If

    set objIE = CreateObject("InternetExplorer.Application")
    objIE.Navigate2 WScript.Arguments(0)

    For intLoop = 1 to (WScript.Arguments.Count-1)

        objIE.Navigate2 WScript.Arguments(intLoop), navFlags

    Next

    objIE.Visible = True
    set objIE = Nothing


If the script is called without any parameters, these will return %errorlevel%=1 , otherwise they will return %errorlevel%=0 .如果在没有任何参数的情况下调用脚本,它们将返回%errorlevel%=1 ,否则它们将返回%errorlevel%=0 No checking is done regarding the "validity" or "availability" of any of the URLs.没有对任何 URL 的“有效性”或“可用性”进行检查。


From within the batch script (or command prompt), call the "JS/VBS" script like this:在批处理脚本(或命令提示符)中,像这样调用“JS/VBS”脚本:

cscript //nologo urls.js "http://bing.com/" "http://google.com/" "http://msn.com/" "http://yahoo.com/"
cscript //nologo urls.vbs "http://bing.com/" "http://google.com/" "http://msn.com/" "http://yahoo.com/"

OR even:甚至:

cscript //nologo urls.js "bing.com" "google.com" "msn.com" "yahoo.com"
cscript //nologo urls.vbs "bing.com" "google.com" "msn.com" "yahoo.com"


If for some reason, you wanted to run these with "wscript" instead, remember to use "start /w" so the exit codes (%errorlevel%) will be returned to your batch script:如果出于某种原因,您想使用“wscript”运行这些,请记住使用“start /w”,以便退出代码(%errorlevel%)将返回到您的批处理脚本:

start /w "" wscript //nologo urls.js "url1" "url2" ...
start /w "" wscript //nologo urls.vbs "url1" "url2" ...


Edit: 21-Sep-2016 编辑: 2016 年 9 月 21 日

There has been a comment that my solution is too complicated.有评论说我的解决方案太复杂了。 I disagree.我不同意。 You pick the JavaScript solution, or the VB Script solution (not both), and each is only about 10 lines of actual code (less if you eliminate the error checking/reporting), plus a few lines to initialize constants and variables.您选择JavaScript解决方案,VB Script解决方案(不是两者),每个解决方案只有大约 10 行实际代码(如果消除错误检查/报告,则更少),加上几行来初始化常量和变量。

Once you have decided (JS or VB), you write that script one time , and then you call that script from batch , passing the URLs , anytime you want to use it, like:一旦你决定(JS或VB),你写的脚本一次,然后调用从脚本batch ,合格URLs ,任何时候你想使用它,如:

 cscript //nologo urls.vbs "bing.com" "google.com" "msn.com" "yahoo.com"

The reason I wrote this answer, is because all the other answers, which work for some people, will fail to work for others, depending on:我写这个答案的原因是因为所有其他对某些人有效的答案对其他人无效,这取决于:

  1. The current Internet Explorer settings for "open popups in a new tab", "open in current/new window/tab", etc... Assuming you already have those setting set how you like them for general browsing, most people would find it undesirable to have change those settings back and forth in order to make the script work.当前 Internet Explorer 的“在新选项卡中打开弹出窗口”、“在当前/新窗口/选项卡中打开”等设置...假设您已经将这些设置设置为您喜欢它们进行一般浏览的方式,大多数人会发现它为了使脚本工作,来回更改这些设置是不可取的。
  2. Their behavior is (can be) inconsistent depending on whether or not there was an IE window already open before the "new" links were opened.它们的行为(可能)不一致,具体取决于在打开“新”链接之前是否已经打开了 IE 窗口。 If there was an IE window (perhaps with many open tabs) already open, then all the new tabs would be added there as well.如果 IE 窗口(可能有许多打开的选项卡)已经打开,那么所有新选项卡也会添加到那里。 This might not be desired.这可能不是所希望的。

The solution I provided doesn't have these issues and should behave the same, regardless of any IE Settings or any existing IE Windows.我提供的解决方案没有这些问题并且应该表现相同,无论任何 IE 设置或任何现有的 IE Windows。 (Please let me know if I'm wrong about this and I'll try to address it.) (如果我对此有错误,请告诉我,我会尽力解决。)

Thanks for the tip Rodger.感谢罗杰的提示。

For me it worked as below:对我来说,它的工作方式如下:

@echo off

start /d "" IEXPLORE.EXE www.google.com

start /d "" IEXPLORE.EXE www.yahoo.com

With the settings in Internet Explorer 8:使用 Internet Explorer 8 中的设置:

  • always open popups in a new tab总是在新标签页中打开弹出窗口
  • a new tab in the current window当前窗口中的新选项卡

marcelo_linhares@hotmail.com marcelo_linhares@hotmail.com

Of course it is an old post but just for people who will find it through a search engine.当然,这是一个旧帖子,但只适用于通过搜索引擎找到它的人。

Another solution is to run it like this for IE9 and later另一种解决方案是为 IE9 及更高版本运行它

iexplore.exe" -noframemerging http://google.com
iexplore.exe" -noframemerging http://gmail.com

-noframemerging means run IE independently. -noframemerging表示独立运行 IE。 For example if you want to run 2 browsers and login as different usernames it will not work if you just run 2 IE.例如,如果您想运行 2 个浏览器并以不同的用户名登录,如果您只运行 2 个 IE,它将无法工作。 But with -noframemerging it will work.但是使用-noframemerging它将起作用。

-noframemerging works for IE9 and later, for early versions like IE8 it is -nomerge -noframemerging适用于 IE9 及更高版本,对于像 IE8 这样的早期版本,它是-nomerge

Usually I create 1 bat file like this run_ie.bat通常我会像这样创建 1 个 bat 文件 run_ie.bat

"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -noframemerging %1

and I create another bat file like this run_2_ie.bat我创建了另一个像这样的 bat 文件 run_2_ie.bat

start run_ie.bat http://google.com
start run_ie.bat http://yahoo.com

This worked for me:这对我有用:

start /d IEXPLORE.EXE www.google.com
start /d IEXPLORE.EXE www.yahoo.com

But for some reason opened them up in Firefox instead?!?但是出于某种原因,在 Firefox 中打开了它们?!?

I tried this but it merely opened up sites in two different windows:我试过了,但它只是在两个不同的窗口中打开了网站:

start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE www.google.com
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE www.yahoo.com

There is a setting in the IE options that controls whether it should open new links in an existing window or in a new window. IE 选项中有一个设置,用于控制是在现有窗口中还是在新窗口中打开新链接。 I'm not sure if you can control it from the command line but maybe changing this option would be enough for you.我不确定你是否可以从命令行控制它,但也许改变这个选项对你来说就足够了。

In IE7 it looks like the option is "Reuse windows for launching shortcuts (when tabbed browsing is disabled)".在 IE7 中,该选项看起来像是“重用窗口以启动快捷方式(禁用选项卡式浏览时)”。

Try this so you allow enough time for the first process to start.. else it will spawn 2 processes because the first one is not still running when you run the second one... This can happen if your computer is too fast..试试这个,这样你就可以有足够的时间让第一个进程启动……否则它会产生 2 个进程,因为当你运行第二个进程时第一个进程没有继续运行……如果你的计算机太快,就会发生这种情况。

@echo off
start /d iexplore.exe http://google.com
PING 1.1.1.1 -n 1 -w 2000 >NUL
START /d iexplore.exe blablabla

replace blablabla with another address用另一个地址替换 blablabla

Thanks Marcelo.谢谢马塞洛。 This worked for me.这对我有用。 I wanted to open a new IE Window and open two tabs in that so I modified the code:我想打开一个新的 IE 窗口并在其中打开两个选项卡,所以我修改了代码:

start iexplore.exe website
PING 1.1.1.1 -n 1 -w 2000 >NUL 
START /d iexplore.exe website

The top answer is almost correct, but you also need to add an ampersand at the end of each line.最佳答案几乎是正确的,但您还需要在每行末尾添加一个与号。 For example write the batch file:例如编写批处理文件:

start /d "~\iexplore.exe" "www.google.com" & 
start /d "~\iexplore.exe" "www.yahoo.com" &
start /d "~\iexplore.exe" "www.blackholesurfer.com" &

The ampersand allows the prompt to return to the shell and launch another tab. &符号允许提示返回到外壳并启动另一个选项卡。 This is a windows solution only, but the ampersand has the same effect in linux shell.这只是一个 windows 解决方案,但&符号在 linux shell 中具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM