Possible search and replace in batches within .LNK file content based on path (e.g. target from C:\App\* to H:\App\*)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Possible search and replace in batches within .LNK file content based on path (e.g. target from C:\App\* to H:\App\*)?

Post by cadudesun » 05 Dec 2021, 12:51

Hi,

So far this script I got from another thread has worked perfectly for me:

Code: Select all

oldDir := "K:", newDir := "H:"
Loop, Files, %A_ScriptDir%\*.lnk
{ FileGetShortcut, %A_LoopFilePath%, target, dir, args, desc, icon, iconNum, state
  If (target ~= oldDir)
   FileCreateShortcut, % StrReplace(target, oldDir, newDir), %A_LoopFilePath%
                     , % StrReplace(dir   , oldDir, newDir)
                     , %args%, %desc%, %icon%,, %iconNum%, %state%
}
MsgBox, 64, Done, Done!
However a new use case has just appeared, and I'd appreciate further help to adjust that script to the following situation:

I have in same folder several shortcuts with different paths, and I just need to batch update fields "target" and "start in" within all .lnk files starting with the path "C:\Apps\*" to "H:\Apps\*", not affecting for instance .lnk with "C:\Program Files\*".

For instance, the link fields Traget and Start in would change to "H:\Apps\App.exe" would change to "H:\Apps\" respectivelly.

Could it be achieved?

Thank you!
Shortcut From-To.png
Shortcut From-To.png (15.02 KiB) Viewed 520 times

User avatar
mikeyww
Posts: 26931
Joined: 09 Sep 2014, 18:38

Re: Possible search and replace in batches within .LNK file content based on path (e.g. target from C:\App\* to H:\App\*

Post by mikeyww » 05 Dec 2021, 14:07

Code: Select all

oldDir := "C:\_Apps\", newDir := "H:\_Apps\"
Loop, Files, %A_ScriptDir%\*.lnk
{ FileGetShortcut, %A_LoopFilePath%, target, dir, args, desc, icon, iconNum, state
  If Instr(target, oldDir)
   FileCreateShortcut, % StrReplace(target, oldDir, newDir), %A_LoopFilePath%
                     , % StrReplace(dir   , oldDir, newDir)
                     , %args%, %desc%, %icon%,, %iconNum%, %state%
}
MsgBox, 64, Done, Done!

cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible search and replace in batches within .LNK file content based on path (e.g. target from C:\App\* to H:\App\*

Post by cadudesun » 05 Dec 2021, 16:08

Hi @mikeyww
Many thanks for sharing the script and helping again!
I just used it updating hundreds of shortcuts.
Cheers

Post Reply

Return to “Ask for Help (v1)”