cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: How to get URL associated with an existing Windows URL shortcut?
by ikegami (Patriarch) on Aug 02, 2023 at 20:46 UTC

    Works for me.

    use v5.14; use warnings; use Win32::OLE qw( ); my $wsh = Win32::OLE->new( 'WScript.Shell' ); sub read_shortcut { my $lnk_path = shift; my $shcut = $wsh->CreateShortcut( $lnk_path ) or die; return $shcut->TargetPath; } say read_shortcut( "a.url" );
    >perl a.pl https://www.perlmonks.org/

    Are you sure you have the right file name?

      Maybe it has something to do with the extension on the shortcut? When windows creates the shortcut it doesn't supply any extension. When I add an extension .url it still doesn't work. And yes I am giving the full path. Go figure!

      EDIT: Well, well...I read the directory and Perl says the shortcut file has extension .url. In File Explorer no extension is shown *even though I have it configured to show extensions* and it shows them for other files. Microsoft trying to "help" me again! :-/

        yeah, extensions of shortcuts have always been hidden, regardless of that setting.

Re: How to get URL associated with an existing Windows URL shortcut?
by Discipulus (Canon) on Aug 03, 2023 at 09:19 UTC
    Hello cormanaz,

    ikegami++ code works both for .link and .url files, but they are different: the first one contains binary data (but it seems that the final path is still human redable) while the second one is a plain text file:

    perl -lne "print if s/URL=//" Perlmonks.url https://perlmonks.org/

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Ah well now that I know this, I won't mess around with these mods, I'll just open the file and get the URL. Thanks!
        If this is long-lived code, I would still use the Win32::OLE module. You never know when the internals of the file structure will change, and then your regex trick stops working.

        But, if you're just making quick scripts to get work done, then yeah do it whichever way is fastest.