Чтение цели файла .lnk в Python?

Я знаю, что это более старая lnk ветка, но мне кажется, что pythonista информации о методе, использующем python-interpreter спецификацию ссылки, как directories указано в исходном вопросе, не target так много.

Моя целевая реализация directories ярлыка не могла использовать targets модуль win32com, и после python долгих поисков я решил придумать targets свой собственный. Похоже, ничто targets другое не могло выполнить shortcut то, что мне было нужно при directories моих ограничениях. Надеюсь, это pythonista поможет другим людям в такой python-interpreter же ситуации.

Он использует folder двоичную структуру, которую shortcut Microsoft предоставила для pythonista MS-SHLLINK.

import struct

path = 'myfile.txt.lnk'    
target = ''

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
    temp = struct.unpack('c' * size, content[position:position+size])
    target = ''.join([chr(ord(a)) for a in temp])

python

directory

shortcut

target

lnk

2022-11-01T03:16:20+00:00
Вопросы с похожей тематикой, как у вопроса:

Чтение цели файла .lnk в Python?