从批处理文件执行快捷链接,不要等待应用程序退出
我有几个需要同时打开的应用程序,而不是仅仅依靠启动文件夹来同时启动它们(因此,如果某些应用程序在一天中的某个时间点关闭,我可以重新打开所有应用程序) )我创建了一个充满快捷方式的文件夹,其方式与 Linux 的运行级别启动链接类似。因此,在文件夹中,我有类似于以下内容的快捷方式链接:
- S00 - Outlook.lnk
- S01 - Notepad++.lnk
- S02 - Chrome.lnk
- S03 - Skype.lnk
我创建了一个批处理文件,它将循环遍历与命名格式匹配的所有链接并启动他们。当前该批处理文件的内容是:
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO "%%f"
它将启动我尝试的大部分链接,但有些链接将启动并等待进程退出,从而阻止进一步的脚本打开。 是否有任何方法可以执行快捷方式,而无需等待批处理文件中的进程退出?或者这是一个失败的原因,我应该寻找 Powershell 解决方案?
到目前为止我尝试过的事情:
将内容更改为
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO START /B /I "%%f"
它在后台进程中启动命令提示符,但从未实际启动链接的目标。
将内容更改为
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO %comspec% /k "%%f"
它启动第一个链接,但等待。
I have several applications that I tend to need open at the same time, and rather than relying solely on the Startup folder to launch them at the same time (and so I can reopen all of them if some were closed at some point throughout the day) I created a folder full of shortcuts in a similar fashion to Linux's runlevel startup links. So in the folder I have shortcut links similar to:
- S00 - Outlook.lnk
- S01 - Notepad++.lnk
- S02 - Chrome.lnk
- S03 - Skype.lnk
I created a batch file that will loop through all the links that match the naming format and launch them. The contents of that batch file currently is:
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO "%%f"
It will launch most of the links I try just fine, but some will launch and wait for the process to exit, thus preventing further scripts from opening. Is there any way to execute the shortcuts without waiting for processes to exit within the batch file? Or is this a lost cause and I should look for a Powershell solution instead?
Things I've tried so far:
Changing the contents to
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO START /B /I "%%f"
It launches the command prompt in a background process, but never actually launches the target of the link.
Changing the contents to
@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO %comspec% /k "%%f"
It launches the first link, but waits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试第一种方法,但将
START
命令中的/B
去掉。这样,该进程将在新窗口中启动,因此不会导致其等待。使用 /B,它将在同一窗口中启动该进程。因此,如果该进程由于某种原因阻塞,那么在它完成之前您将无法恢复控制权。例如,考虑以下批处理文件 foo.bat(为此您需要 sleep 命令,我有 cygwin,因此它附带了):
从命令提示符处,如果您键入,
您会注意到控件不会返回到命令提示符直到睡眠完成。但是,如果我们这样做:
那么控制权会立即返回(因为 foo.bat 在新窗口中启动),这就是您想要的。
Try the first way, but take the
/B
off of theSTART
command. That way, the process will launch in a new window, so it shouldn't cause it to wait.With the /B, it's going to launch the process in the same window. So if that process blocks for some reason, then you won't get control back until it finishes. For example, consider the following batch file, foo.bat (you need sleep command for this, I have cygwin so it comes with that):
From a command prompt, if you type
you'll notice that control won't come back to the command prompt until the sleep finishes. However, if we do this:
then control comes back immediately (because foo.bat starts in a new window), which is what you want.
dcp 的答案 通过尝试不使用
/B
标志来指出正确的方向,但这不是解决方案。显然,无论传递给它的参数的顺序如何,START
都会采用第一个带引号的字符串作为新窗口的标题,而不是假设它是可执行文件/命令。因此,当START
进程启动时,它们将快捷方式链接作为窗口的标题,并且起始目录是主批处理文件的工作目录。因此,我将文件更新为批处理文件,如下所示,它可以工作:dcp's answer pointed in the right direction by trying it without the
/B
flag, but it wasn't the solution. ApparentlySTART
will take the first quoted string, regardless of the order of parameters passed to it, as the title of the new window rather than assuming it is the executable/command. So when theSTART
processes launched they had the shortcut links as the title of the window, and the starting directory was the working directory of the main batch file. So I updated my file to batch file to the following and it works: