Creating Shortcuts using a For Loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 139
Joined: 17 Aug 2015 06:05

Creating Shortcuts using a For Loop

#1 Post by batchcc » 17 Aug 2020 17:04

I have a directory R:\Archived Folders\ which contains several thousand sub folders. I have another folder H:\WORK\ which has the many sub folders that have the same name as those in the archive. I would like to create a link to each archive folder in each corresponding sub folder in work. I have tried the following code but it isn't working.

Code: Select all

@echo off
for %%X in (dir "R:\Archived Folders\*" /AD /B) do (IF EXIST "H:\WORK\%%X"(cd /d "H:\WORK\%%X" && Shortcut.exe /F:"%cd%\Archived Folder - %%X.lnk" /A:C /T:"%%X"))

I would greatly appreciate any help. I am using Windows 10 if that matters. I should also note that the sub folders in the WORK folder also have sub folders but I do not wish to have links to those. (like links to the children of H:\WORK but not the grandchildren)

Additionally, shortcut.exe can be found here:

Code: Select all

The syntax of this command is:

Shortcut.exe /F:filename /A:C|E|Q [/T:target] [/P:parameters] [/W:workingdir]
         [/R:runstyle] [/I:icon,index] [/H:hotkey] [/D:description]

 /F:filename    : Specifies the .LNK shortcut file.
 /A:action      : Defines the action to take (C=Create, E=Edit or Q=Query).
 /T:target      : Defines the target path and file name the shortcut points to.
 /P:parameters  : Defines the command-line parameters to pass to the target.
 /W:working dir : Defines the working directory the target starts with.
 /R:run style   : Defines the window state (1=Normal, 3=Max, 7=Min).
 /I:icon,index  : Defines the icon and optional index (file.exe or file.exe,0).
 /H:hotkey      : Defines the hotkey, a numeric value of the keyboard shortcut.
 /D:description : Defines the description (or comment) for the shortcut.

 - Any argument that contains spaces must be enclosed in "double quotes".
 - If Query is specified (/A:Q), all arguments except /F: are ignored.
 - To find the numeric hotkey value, use Explorer to set a hotkey and then /A:Q
 - To prevent an environment variable from being expanded until the shortcut
   is launched, use the ^ carat escape character like this: ^%WINDIR^%

   /f:"%ALLUSERSPROFILE%\Start Menu\Programs\My App.lnk" /a:q
   /f:"%USERPROFILE%\Desktop\Notepad.lnk" /a:c /t:^%WINDIR^%\Notepad.exe /h:846
   /f:"%USERPROFILE%\Desktop\Notepad.lnk" /a:e /p:C:\Setup.log /r:3

Posts: 4198
Joined: 23 Dec 2011 13:59

Re: Creating Shortcuts using a For Loop

#2 Post by Squashman » 18 Aug 2020 07:05

With the FOR command you are missing two critical components. You need to use the /F option and you need to enclose the command within single quotes.

Code: Select all

for /F "delims=" %%X in ('dir "R:\Archived Folders\*" /AD /B') do
The delims option will also be needed because of spaces in the output of the command.

Posts: 139
Joined: 17 Aug 2015 06:05

Re: Creating Shortcuts using a For Loop

#3 Post by batchcc » 20 Aug 2020 19:38

Thank you Squashman!

Jim W.
Posts: 6
Joined: 28 May 2017 20:46

Re: Creating Shortcuts using a For Loop

#4 Post by Jim W. » 24 Sep 2020 01:26

You could also do it in batch and VBS with:

Code: Select all

>makeAwSimBatLnk.vbs (
echo Dim WshShell
echo set wshShell = WScript.CreateObject^( "WScript.Shell" ^)
echo Set oLink = WSHShell.CreateShortcut^("AwSim.lnk"^)
echo oLink.TargetPath = "cmd.exe"
echo oLink.Arguments = "/C AwSim.bat"
echo oLink.WorkingDirectory = "%~dp0%"
echo oLink.WindowStyle = 7
echo oLink.IconLocation = "%~dp0AwSim.ico"
echo oLink.Save
and then run:
call wscript.exe makeAwSimBatLnk.vbs

Post Reply