<?xml version="1.0" encoding="windows-1252"?>
<node id="263858" title="Read and write Windows &quot;shortcut&quot; links" created="2003-06-06 16:48:15" updated="2005-08-02 00:49:00">
<type id="1980">
snippet</type>
<author id="170442">
jdporter</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
We know that Windows "shortcuts" aren't really symbolic links; only Windows Explorer and certain other applications can make sense of them.
Now your perl program can, too.

This uses OLE to access the contents of a shortcut.</field>
<field name="snippetcode">
&lt;CODE&gt;

use Win32::OLE;

my $wsh = new Win32::OLE 'WScript.Shell';


sub read_shortcut {
    my $lnk_path = shift; # path of existing .lnk file

    my $shcut = $wsh-&gt;CreateShortcut($lnk_path) or die;
    $shcut-&gt;TargetPath
}


sub write_shortcut {
    my $lnk_path = shift; # path of new .lnk file
    my $target_path = shift;
    my $comment = shift; # optional

    my $shcut = $wsh-&gt;CreateShortcut($lnk_path) or die;
    $shcut-&gt;{'TargetPath'} = $target_path;
    $shcut-&gt;{'Description'} = $comment if defined $comment;
    $shcut-&gt;Save;
}


print "c:\\foo.lnk points to ", read_shortcut("c:\\foo.lnk");

# In fact, the shortcut file can be .url too, not just .lnk,
# in which case it is an "internet shortcut", and the target
# path is a URL.

write_shortcut( "c:\\perlmonks.url", "http://perlmonks.org" );

&lt;/CODE&gt;</field>
</data>
</node>
