Execute shortcut links from a batch file and don&#

2019-04-07 23:46发布

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.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-04-08 00:09

Try the first way, but take the /B off of the START 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):

echo hi
sleep 5

From a command prompt, if you type

START /B foo.bat

you'll notice that control won't come back to the command prompt until the sleep finishes. However, if we do this:

START foo.bat

then control comes back immediately (because foo.bat starts in a new window), which is what you want.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-08 00:21

dcp's answer pointed in the right direction by trying it without the /B flag, but it wasn't the solution. Apparently START 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 the START 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:

@FOR /F "usebackq delims==" %%f IN (`dir /b "S* - *.lnk"`) DO START /B /I "TITLE" "%%f"
查看更多
登录 后发表回答