Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute shortcut links from a batch file and don't wait for application to exit

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.

like image 335
Agent_9191 Avatar asked May 26 '11 13:05

Agent_9191


People also ask

How do I run a shortcut in a batch file?

If you want to use a custom file icon for your batch file, we recommend using a shortcut. Right-click the desktop and select New > Shortcut. Choose a location, ideally the same as your batch file, and click Next. Then enter a name for the shortcut and click Finish.

How do I run a batch file without closing a window?

If you're creating a batch file and want the MS-DOS window to remain open, add PAUSE to the end of your batch file. This prompts the user to Press any key. Until the user presses any key, the window remains open instead of closing automatically.

How do I add a delay in a batch file?

Type in your command. TIMEOUT — Type timeout time where "time" is replaced by the number of seconds to delay. For example, typing in timeout 30 will delay your batch file for 30 seconds.

How do I stop command prompt from closing after batch file?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.


1 Answers

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.

like image 108
dcp Avatar answered Nov 15 '22 12:11

dcp