-
-
Save Winand/997ed38269e899eb561991a0c663fa49 to your computer and use it in GitHub Desktop.
import locale | |
import struct | |
def __readLink(path): | |
# http://stackoverflow.com/a/28952464/1119602 | |
with open(path, 'rb') as stream: | |
content = stream.read() | |
# skip first 20 bytes (HeaderSize and LinkCLSID) | |
# read the LinkFlags structure (4 bytes) | |
lflags = struct.unpack('I', content[0x14:0x18])[0] | |
position = 0x18 | |
# if the HasLinkTargetIDList bit is set then skip the stored IDList | |
# structure and header | |
if (lflags & 0x01) == 1: | |
position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E | |
last_pos = position | |
position += 0x04 | |
# get how long the file information is (LinkInfoSize) | |
length = struct.unpack('I', content[last_pos:position])[0] | |
# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags and VolumeIDOffset) | |
position += 0x0C | |
# go to the LocalBasePath position | |
lbpos = struct.unpack('I', content[position:position+0x04])[0] | |
position = last_pos + lbpos | |
# read the string at the given position of the determined length | |
size = (length + last_pos) - position - 0x02 | |
content = content[position:position+size].split(b'\x00', 1) | |
return content[-1].decode('utf-16' if len(content) > 1 | |
else locale.getdefaultlocale()[1]) |
better performance for multiple shortcuts, running everything in 1 powershell cmd.
get all links, recursively in a folder
nearly instant instead of wait a few seconds when running on all shortcuts in windows start menu
def get_target_and_args_folder(folder) -> (str, str):
"""
Get the target & args of all Windows shortcuts (.lnk) in a folder
:param link_path: The Path or string-path to the shortcut, e.g. "C:\\Users\\Public\\Desktop\\My Shortcut.lnk"
:return: A tuple of the target and arguments, e.g. ("C:\\Program Files\\My Program.exe", "--my-arg")
"""
# see https://gist.github.com/Winand/997ed38269e899eb561991a0c663fa49
ps_command = \
"$Shortcuts = Get-ChildItem -Recurse \"" + str(folder) + "\" -Include *.lnk;" \
"$WSShell = New-Object -ComObject Wscript.Shell;" \
"foreach ($ShortcutPath in $Shortcuts)" \
"{" \
"$Shortcut = $WSShell.CreateShortcut($ShortcutPath);" \
"Write-Host $Shortcut.TargetPath ';' $shortcut.Arguments " \
"}"
output = subprocess.run(["powershell.exe", ps_command], capture_output=True)
raw = output.stdout.decode('utf-8')
result = []
for line in raw.splitlines():
launch_path, args = [x.strip() for x in line.split(';', 1)]
result.append((launch_path, args))
return result
@hannesdelbeke yes, it's very basic implementation which does not work with some .lnk files.
So I switched to ShellLink.GetPath COM method. You just create IShellLink() object and call get_path(path_to_link)
.
see also IShellLink docs
Thanks for the update!
if i'm not mistaken, the python implementation for that uses win32com, which is not in the standard python library.
Thanks for the update! if i'm not mistaken, the python implementation for that uses win32com, which is not in the standard python library.
No no, com_interfaces is a package i've made recently. It uses only ctypes
. I've added IShellLink
as an example. You can implement other interfaces in a similar way (using Microsoft docs, etc.)
Pros:
Cons:
e.g. for
C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Blackmagic Design\DaVinci Resolve\Blackmagic Proxy Generator Lite.lnk
it returns the target path correctly instead of Noneshared this in the original stackoverflow thread
Notes:
some .lnk files still return None. e.g.
C:\Users\hanne\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\System Tools\run.lnk
that 's because they have an empty target which you can confirm in the properties of the shortcut
url lnks to http URLs return None too.
TODO: this needs addressing.