Good day Brothers/Sisters. I am trying to accomplish something that seems like it should be an easy task, getting a URL out of a Windows shortcut file. But as with so many things Windows, what should be easy isn't.

Following suggestions in node Read and write Windows "shortcut" links I have tried doing it with Image::ExifTool and Win32::OLE (had trouble installing the other two mods suggested):

use strict; use feature ':5.10'; use Data::Dumper; use Image::ExifTool qw(:Public); use Win32::OLE; # cwd is c:/tmp/ say "exiftool\n"; my $info = ImageInfo("c:/tmp/test/link.lnk"); say Dumper($info); say "\nOLE\n"; my $wsh = new Win32::OLE 'WScript.Shell'; my $shcut = $wsh->CreateShortcut("c:/tmp/test/link.lnk") or die; say $shcut->TargetPath

This is pointing to a shortcut in a subfolder test where I inserted "https://perlmonks.org" in the box labeled "Type the location of this item:" and "link" in "Type a name for this shortcut:"

The first approach yields "error opening file" and the second one dies. I was a little skeptical of the code for the second because the method is "CreateShortcut" whereas I am trying to read one.

I tried changing the name of the link to link.lnk and tried again. No difference for the first approach. The second one didn't die, but returned null.

So then I think to myself: "Self, maybe these are just designed for links to other folders, etc. (equivalent of Linux symlinks) and not URL shortcuts." So I copy another folder and paste it as a shortcut in ../test, point the above script at that, and voila! Both approaches work

So does anyone know how I can get the URL out of a shortcut that contains one instead of another filesystem locaiton?


In reply to How to get URL associated with an existing Windows URL shortcut? by cormanaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.