zhekaus
zhekaus

Reputation: 3194

How to run several shortcuts (.lnk files) with a script in Windows?

I’m trying to create a script to run all the tools I use working in a particular project (dor instance, gulp, phpstorm, php server, etc.)

I’d like to run shortcuts but not executable files. I can see two possible ways to achieve it in Windows:

  1. VbScript
  2. cmd.exe

Unfortunately, I’ve failed both:

1) WshShell fire .lnk file and I can't find an alternative way to do that:

To do so I’m trying to write .cmd file or .vbs However, I’ve got the problem in each way.

Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe" ' Works alright
WshShell.Run "C:\Users\eugene\Desktop\gulp EngMe.lnk" ' got an Exception: Unknown Exception

2) .cmd script waits for closing previously started app:

@rem urls and filesystem shortcuts start alright:
"C:\Users\eugene\Desktop\8000.url"
"C:\Users\eugene\Desktop\project_folder.lnk"

@rem the following two commands are starting sequentially, when the previous one has closed.
"notepad.exe"
@rem shortcut to %windir%\system32\notepad.exe:
"C:\Users\eugene\Desktop\Notepad_.lnk"

What I'm doing wrong and is there another way to do what I'm trying?

Upvotes: 3

Views: 12099

Answers (3)

Alex K.
Alex K.

Reputation: 175748

Paths with spaces must be "quoted" when fed to .Run so:

WshShell.Run """C:\Users\eugene\Desktop\gulp EngMe.lnk"""

Will work fine.

Upvotes: 3

npocmaka
npocmaka

Reputation: 57242

You can check the shortcutjs.bat which is capable to read a .lnk file properties. With this you will execute all .lnk files in the current directory in the correct working directory:

@echo off

for %%# in (*.lnk) do (
    call :executeShortcut "%%#"
)

exit /b %errorlevel%
:executeShortcut - %1 - shortcut to execute
setlocal enableDelayedExpansion
for /f "tokens=1,* delims=:" %%a in ('call shortcutjs.bat -examine %1') do (
    if /i "%%a" equ "Target" (
        set "target=%%b"
        set "target=!target:~1!"
    )

    if /i "%%a" equ "Working Directory" (
        set "workdir=%%b"       
        set "workdir=!workdir:~1!"
    )
)

start "%target%" /d "%workdir%" /wait /b "%target%"
exit /b %errorlevel%

Upvotes: 1

mrwes
mrwes

Reputation: 639

Try using powershell for scripting.

Look at the question - Execute shortcuts like programs

Look at Mark Schill's answer using invoke-item to launch .lnk files like programs.

This is probably your best way to address your script challenges.

Upvotes: 2

Related Questions