Launch a shortcut using batch file

Here's the problem.

I have a shortcut on roughly 260 computers located in the same folder as below;

"c:\documents and settings\all users\desktop\Remote agent 1234.lnk"

The only thing that changes on the shortcut is the number. Is there a way to use a wildcard in a batch file to launch the shortcut instead of having to specify the full file name?


Solution 1:

You can use either for or forfiles for this task. Forfiles is more flexible, but it might not work properly on older versions of Windows.

For

From a command prompt:

for %a in ("C:\Documents and Settings\All Users\Desktop\Remote agent *.lnk") do @start "" "%a"

In a batch file:

for %%a in ("C:\Documents and Settings\All Users\Desktop\Remote agent *.lnk") do @start "" "%%a"

Forfiles

forfiles /P "C:\Documents and Settings\All Users\Desktop" /M "Remote agent *.lnk" /C "cmd /C start \"\" @path"

Forfiles goes through all files in the path specified in /P that match the mask specified on /M and executes the command specified in /C. Here @path is the full path of the file.

Normally, we'd use the command start "" "Remote Agent 1234.lnk" to launch the shortcut. Since start is an internal command, we have to call it in a new shell (cmd /C). \"\" are just escaped double quotes, since the entire string is already quoted.