0

I was looking at several sites that had similar questions, but none applied to my situation. As far as I know, vbscript is the best way to do what I'm looking for, but I have very little experience with vbscript, and don't know how to get what I want. I am using Windows 10.

Let's say that there is an example shortcut named Example, located in F:\File\SubFile\. Within the SubFile folder there are more folder, F:\File\SubFile\PrgmFiles\Icons\. Within Icons there is one file, Icon.ico. The shortcut Example has the icon: Icon.ico. However, the folder SubFile is moved to an unknown location, *\SubFile. The shortcut loses it's icon because the path has changed, but not the relative path to the icon.

What I want to know is how to make a script that would take it's current location, *\SubFile, and apply Icon.ico to the example shortcut.

2 Answers2

1

A .lnk file can store the relative path to its target but it does not support relative icon paths.

It supports a normal absolute icon path and that plain path can optionally be overridden by a path with expandable environment variables (SLDF_HAS_EXP_ICON_SZ).

Anders
  • 97,548
  • 12
  • 110
  • 164
  • But how does this apply to my situation? It seems that you said that it only supports a specific path. What I'm looking for is a way to use a the path of the script being used, and then expand upon it. Like in batch: `cd %~dp0\PrgmFiles\Icons`, only in a way that allows me to edit the icon property of the shortcut. – MainframeHax Apr 22 '17 at 12:04
  • The icon path is not relative to the .lnk in anyway, relative icon paths are not supported! – Anders Apr 22 '17 at 12:07
  • So... your answer doesn't answer my question... so why did you answer it if you didn't know the answer? – MainframeHax Apr 23 '17 at 15:35
  • All you did was state what I already know, and what I need the script for... You just restated the question... – MainframeHax Apr 23 '17 at 15:58
  • According to [this question](http://stackoverflow.com/questions/3418541/how-do-i-create-a-shortcut-lnk-with-a-relative-target) a .lnk can have a relative path, however you have to code it yourself, but it doesn't work on icons. – MainframeHax Apr 23 '17 at 16:05
  • I already said the path to the target can be relative but the path to the icon cannot. The answer is, the icon path cannot be relative because it is not supported by Windows. – Anders Apr 23 '17 at 18:06
1

When you/someone moves/prunes a path with vital information this isn't a programming related issue and off topic here. Nevertheless:

  • First step to repair the broken IconLocation in the .lnk files is to identify them. This PowerShell script wil do:

$SearchRoot = 'C:\Users\UserName'
$AllLnks = Get-ChildItem -Path $SearchRoot -Rec -Filter *.lnk -Force -EA 0
$Wsh = New-Object -ComObject WScript.Shell
ForEach ($Lnk in $AllLnks) {
    $LnkO = $Wsh.CreateShortcut($Lnk.FullName)
    if ($LnkO.IconLocation){
        $Icon = $LnkO.IconLocation.split(',')[0]
        if ($Icon){
            if (!(Test-Path $([System.Environment]::ExpandEnvironmentVariables($Icon) ) ) ){
                $LnkO
            }
        }
    }
}

Sample output with here absent icon files:

FullName         : C:\Users\UserName\Desktop\Programm Links\Adobe Reader XI.lnk
Arguments        :
Description      :
Hotkey           :
IconLocation     : C:\Windows\Installer\{AC76BA86-7AD7-1031-7B44-AB0000000001}\SC_Reader.ico,0
RelativePath     :
TargetPath       : C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe
WindowStyle      : 1
WorkingDirectory :

FullName         : C:\Users\UserName\Desktop\Programm Links\IDLE (Python 3.4 GUI - 32 bit).lnk
Arguments        : "C:\Program Files (x86)\Python\34\Lib\idlelib\idle.pyw"
Description      :
Hotkey           :
IconLocation     : C:\WINDOWS\Installer\{CCD588A7-8D55-49F1-A30C-47FAB40889ED}\python_icon.exe,0
RelativePath     :
TargetPath       : C:\WINDOWS\Installer\{CCD588A7-8D55-49F1-A30C-47FAB40889ED}\python_icon.exe
WindowStyle      : 1
WorkingDirectory : C:\Program Files (x86)\Python\34\
  • I was only giving an example situation, so I could use the code in future situations, as well as the current one. In the case that led me to ask this question is one where the program is located on a portable drive, and when plugged into other computers the drive is labeled differently. Such as on computer 1 it is the X: drive, while on computer 2 it is the F: drive. – MainframeHax Apr 23 '17 at 15:34
  • If I went to a non-programming site and asked this question, people wouldn't know how to answer my question. They would say, "What is vbscript?" "This is a programming related issue and off topic here!" When I am looking for a script that automates the process of setting the icon of a shortcut, regardless of it's location, I believe that requires some programming skill. – MainframeHax Apr 23 '17 at 15:44
  • Would have been great to have this in functional form so one can supply a path or filename and get the results. – not2qubit Jan 12 '20 at 15:00