Emi
Emi

Reputation: 545

Read the Target of a Shortcut using Go

I'm trying to read the target file/directory of a shortcut (.lnk) file using Go.

I already have a loop for all the files in a directory and I can successfully identify if it is a dir with IsDir() or if it is a file IsRegular(). Now I need a way to read if it is a link and, if it is a .lnk, the path of it so I can print it.

I couldn't find any way of doing this and I've been searching on SO but nothing comes up. Any idea?

Upvotes: 4

Views: 807

Answers (1)

VonC
VonC

Reputation: 1326554

You need to read the lnk binary format as defined by Microsoft

In Go, its structure would translate to (as used in the exponential-decay/shortcuts)

//structs that make up the shortcut specification [76 bytes] 
type ShellLinkHeader struct {
   HeaderSize  [4]byte           //HeaderSize
   ClassID     [16]byte          //LinkCLSID
   LinkFlags   uint32            //LinkFlags      [4]byte
   FileAttr    uint32            //FileAttributes [4]byte
   Creation    [8]byte           //CreationTime
   Access      [8]byte           //AccessTime
   Write       [8]byte           //WriteTime
   FileSz      [4]byte           //FileSize
   IconIndex   [4]byte           //IconIndex
   ShowCmd     [4]byte           //ShowCommand

   //[2]byte HotKey values for shortcut shortcuts
   HotKeyLow   byte              //HotKeyLow
   HotKeyHigh  byte              //HotKeyHigh

   Reserved1   [2]byte           //Reserved1
   Reserved2   [4]byte           //Reserved2
   Reserved3   [4]byte           //Reserved3
}

That project should give you an idea to how to decode the shortcut target.

Upvotes: 4

Related Questions