Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
forfiles /P "C:\Documents and Settings\All Users\Desktop" /M "Remote agent *.lnk" /C "cmd /C start \"\" @path" lists the files in the folder but does not open the shortcut. –  ghost3h Feb 15 '13 at 17:48
    
That's odd. Echoing filenames is the default action, but the /C switch overrides that. Which version of Windows? –  Dennis Feb 15 '13 at 17:53
    
Xp Professional, its listing all file extensions as well which I assume isn't normal? –  ghost3h Feb 15 '13 at 17:55
    
Doesn't look like its supported Dennis, again it just echos the files within the directory its run from. Thanks for the help thus far though! –  ghost3h Feb 15 '13 at 18:03
    
I just found a source saying that some older versions of forfiles use - for switches instead of / (very atypical for Windows). Maybe this works: forfiles -P"C:\Documents and Settings\All Users\Desktop" -M"Remote agent *.lnk" -C"cmd /C start \"\" @path" –  Dennis Feb 15 '13 at 18:03
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.