[Solved] Windows: creating lots of shortcuts

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Moderators General, Prelates

User avatar
Felstaff
Occam's Taser
Posts: 5175
Joined: Fri Feb 01, 2008 7:10 pm UTC
Location: ¢ ₪ ¿ ¶ § ∴ ® © ™ ؟ ¡ ‽ æ Þ ° ₰ ₤ ಡಢ

[Solved] Windows: creating lots of shortcuts

Postby Felstaff » Wed May 22, 2019 9:41 am UTC

I'm trying to work out how to create a batch of 1,000+ shortcuts (.lnk's) in Windows, creating a shortcut for each file in a separate folder.

Any help...?

Here's the lowdown:

  • Folder contains 1,000+ files
  • Need to create a shortcut for all of these files in a different folder (so far, so drag-n-drop, right)
  • Each shortcut needs a set of command lines in the 'Target' field: the program (unchanging), the argument (unchanging), the DLL file (unchanging) and the filename (always different)

    Code: Select all

    "C:\Prog.exe" -CommandLineArgumentX "C:\SameDLLForAllFiles.dll" "C:\Folder\File_0001.bin"
    "C:\Prog.exe" -CommandLineArgumentX "C:\SameDLLForAllFiles.dll" "C:\Folder\File_0002.bin"
    "C:\Prog.exe" -CommandLineArgumentX "C:\SameDLLForAllFiles.dll" "C:\Folder\random_file.bin"
How do I create a big ol' batch of shortcuts that takes each file and adds all the parameters in the order <prog><argument><dll><filename>...?

Image

If there's a program that can do this, or an easy way to do it in PowerShell or with a batch file, that would be grand, too.
Away, you scullion! you rampallion! You fustilarian! I'll tickle your catastrophe.

commodorejohn
Posts: 1176
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Windows: creating lots of shortcuts

Postby commodorejohn » Wed May 22, 2019 1:26 pm UTC

Ooh, good question. Windows does have a command-line utility for making plain symbolic links, but I don't know if there's one for full-fledged parametered shortcut files. That might be a PowerShell thing.
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

User avatar
Flumble
Yes Man
Posts: 2227
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Windows: creating lots of shortcuts

Postby Flumble » Wed May 22, 2019 3:24 pm UTC

I assume you have no problem making a loop to traverse all the files in a directory. (I used to, but then forgot and right now I can't be arsed to look it up for either batch, powershell or other shell.)

Idea 1: use VBScript to create the shortcuts. It might be easiest to create the whole directory walking with a batch/powershell script and only making the link with VBScript.

Code: Select all

makelink.vbs
'''Call as: cscript makelink.vbs linkfile original
target = WScript.Arguments(0)
source = WScript.Arguments(1)
Set oWS = WScript.CreateObject("WScript.Shell")
Set oLink = oWS.CreateShortcut(target)

oLink.TargetPath = "C:\Prog.exe"
  oLink.Arguments = "-CommandLineArgumentX ""C:\SameDLLForAllFiles.dll"" """ & source & """"
'  oLink.WorkingDirectory = "might be needed"
oLink.Save

The same may very well be written in powershell with slightly different syntax, but that's left as an exercise for the reader. :mrgreen:


Idea 2: make a custom file handler for your files. Making the links becomes optional.

Code: Select all

felstafffiles.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.felstaff]
@="felstafffile"
[HKEY_CLASSES_ROOT\felstafffile]
[HKEY_CLASSES_ROOT\felstafffile\shell]
[HKEY_CLASSES_ROOT\felstafffile\shell\Open]
[HKEY_CLASSES_ROOT\felstafffile\shell\Open\command]
@="\"C:\\Prog.exe\" -CommandLineArgumentX \"C:\\SameDLLForAllFiles.dll\" \"%1\""

Now if your files have a particular extension that's not bound to anything (.bin1 seems particular), you can simply replace the .felstaff with .bin1 and be done. Otherwise, if the original (bound) file extension needs to be preserved, you can make hardlinks in the other folder with a different extension: mklink /h "{other\directory}{originalfile}.felstaff" "{original\directory\originalfile.ext}".

User avatar
Jorpho
Posts: 6276
Joined: Wed Dec 12, 2007 5:31 am UTC
Location: Canada

Re: Windows: creating lots of shortcuts

Postby Jorpho » Wed May 22, 2019 3:55 pm UTC

My instinct would be to reach for Python.
https://www.blog.pythonlibrary.org/2010 ... shortcuts/

Flumble wrote:I assume you have no problem making a loop to traverse all the files in a directory. (I used to, but then forgot and right now I can't be arsed to look it up for either batch, powershell or other shell.)
The "forfiles" command became a standard component in Windows Vista.
https://en.wikipedia.org/wiki/Forfiles

User avatar
Felstaff
Occam's Taser
Posts: 5175
Joined: Fri Feb 01, 2008 7:10 pm UTC
Location: ¢ ₪ ¿ ¶ § ∴ ® © ™ ؟ ¡ ‽ æ Þ ° ₰ ₤ ಡಢ

Re: Windows: creating lots of shortcuts

Postby Felstaff » Wed May 22, 2019 5:56 pm UTC

Thanks for your help thus far. I'm not a Batcher or regular user of VBA scripts. I've used PowerShell to generally rename files in bulk before (with mixed results).

I have, gleaning from StackOverflow, edited the following batch file:

Code: Select all

@echo off

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "C:\TargetFolder\shortcut.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "C:\prog.exe" >> %SCRIPT%
echo oLink.Arguments = "-X ""C:\dll.dll""" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%


Running it creates a single shortcut called "shortcut" and the target argument contains "C:\prog.exe" -X "C:\dll.dll", which is 75% correct. Yet it's still missing the final 25% - the file location at the end of the argument.

My questions now are:
  • how do I edit sLinkFile so that "shortcut.lnk" is any (all) filenames in the original folder? (i.e. File0001.lnk, File0002.lnk, random_file.lnk, etc.)
  • How do I add the file location (C:\Folder\File_0001.bin) at the end of the command line arguments?
  • How do I get it to apply all o' this to every file in the original folder? (i.e. create 1000+ shortcuts in one fell swoop)

Flumble wrote:I assume you have no problem making a loop to traverse all the files in a directory. (I used to, but then forgot and right now I can't be arsed to look it up for either batch, powershell or other shell.)

I do have a problem, but I can figure that out later (a cursory skim of stackoverflow suggests I may have significant problems with this, but I'll deal with that later.

Flumble wrote:Idea 1: use VBScript to create the shortcuts. It might be easiest to create the whole directory walking with a batch/powershell script and only making the link with VBScript.

Code: Select all

makelink.vbs
'''Call as: cscript makelink.vbs linkfile original
target = WScript.Arguments(0)
source = WScript.Arguments(1)
Set oWS = WScript.CreateObject("WScript.Shell")
Set oLink = oWS.CreateShortcut(target)

oLink.TargetPath = "C:\Prog.exe"
  oLink.Arguments = "-CommandLineArgumentX ""C:\SameDLLForAllFiles.dll"" """ & source & """"
'  oLink.WorkingDirectory = "might be needed"
oLink.Save

That was sort of the idea I was thinking of. I saved that as a vbs and got an error straight of the bat - (Line 1, char 1)
Away, you scullion! you rampallion! You fustilarian! I'll tickle your catastrophe.

User avatar
Flumble
Yes Man
Posts: 2227
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Windows: creating lots of shortcuts

Postby Flumble » Thu May 23, 2019 12:29 am UTC

Jorpho wrote:The "forfiles" command became a standard component in Windows Vista.
https://en.wikipedia.org/wiki/Forfiles

Whoever thought forcing quotes in the output was a good idea needs to reconsider their place in the universe. :?
So I'm going back to the old batch loops:

Code: Select all

rem replace %indir% and %outdir% with the paths you want
for /r "%indir%" %%i in (*) do (
cscript makelink.vbs "%outdir%\%%~ni.lnk" "%%~i"
)

The magic here is that %~ni expands to just the file name, not the extension or path.

Felstaff wrote:I saved that as a vbs and got an error straight of the bat - (Line 1, char 1)

You're not supposed to copy "makelink.vbs", that's just an example filename. :wink: (if that's not the issue: make sure you call the script with arguments or it'll complain about "subscript out of range".)

Jorpho wrote:My instinct would be to reach for Python.
https://www.blog.pythonlibrary.org/2010 ... shortcuts/

Yeah, the programmer in me is screaming at the ugliness of batch and vbscript. But it does get the job done without installing a whole python environment. :oops:


Irregardlessly, the powershell "one"-liner looks decent, so forget everything else:

Code: Select all

Get-ChildItem 'I:\folder\with\originals' | ForEach-Object {
   $original = '"' + $_.FullName + '"'
   $link     = 'O:\folder\for\links\' + $_.BaseName + '.lnk'
   $wshell   = New-Object -ComObject WScript.Shell
   $shortcut = $wshell.CreateShortcut($link)
   $shortcut.TargetPath = 'C:\Prog.exe'
   $shortcut.Arguments = '-CommandLineArgumentX "C:\SameDLLForAllFiles.dll" ' + $original
   $shortcut.Save()
}


[edit]fixed "linkPath"
Last edited by Flumble on Fri May 24, 2019 1:37 pm UTC, edited 3 times in total.

User avatar
Felstaff
Occam's Taser
Posts: 5175
Joined: Fri Feb 01, 2008 7:10 pm UTC
Location: ¢ ₪ ¿ ¶ § ∴ ® © ™ ؟ ¡ ‽ æ Þ ° ₰ ₤ ಡಢ

Re: Windows: creating lots of shortcuts

Postby Felstaff » Fri May 24, 2019 10:33 am UTC

Flumble wrote:Irregardlessly, the powershell "one"-liner looks decent, so forget everything else:

Code: Select all

Get-ChildItem 'I:\folder\with\originals' | ForEach-Object {
   $original = '"' + $_.FullName + '"'
   $link     = 'O:\folder\for\links\' + $_.BaseName + '.lnk'
   $wshell   = New-Object -ComObject WScript.Shell
   $shortcut = $wshell.CreateShortcut($link)
   $shortcut.linkPath = 'C:\Prog.exe'
   $shortcut.Arguments = '-CommandLineArgumentX "C:\SameDLLForAllFiles.dll"' + $original
   $shortcut.Save()
}

Genius, an elegant solution. Thank you.

One exception - I had to change $shortcut.linkPath to $shortcut.TargetPath as it threw up an error and created a host of correctly-named, but entirely empty shortcuts.

Flumble wrote:make sure you call the script with arguments or it'll complain about "subscript out of range".)

That's the error I kept getting. It was becoming a real noggin-scratcher, but that's all moot now.

Thanks, again.
Away, you scullion! you rampallion! You fustilarian! I'll tickle your catastrophe.

User avatar
Flumble
Yes Man
Posts: 2227
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Windows: creating lots of shortcuts

Postby Flumble » Fri May 24, 2019 1:37 pm UTC

Felstaff wrote:One exception - I had to change $shortcut.linkPath to $shortcut.TargetPath as it threw up an error and created a host of correctly-named, but entirely empty shortcuts.

Oops, I replaced all target with link (because source and target are very confusing names when it comes to links) in the forum message. :oops: Good thing you figured it out.


Return to “The Help Desk”

Who is online

Users browsing this forum: No registered users and 6 guests