Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Read and write Windows "shortcut" links

by jdporter (Canon)
on Jun 06, 2003 at 20:48 UTC ( #263858=snippet: print w/ replies, xml ) Need Help??

Description: 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.

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->CreateShortcut($lnk_path) or die;
    $shcut->TargetPath
}


sub write_shortcut {
    my $lnk_path = shift; # path of new .lnk file
    my $target_path = shift;
    my $comment = shift; # optional

    my $shcut = $wsh->CreateShortcut($lnk_path) or die;
    $shcut->{'TargetPath'} = $target_path;
    $shcut->{'Description'} = $comment if defined $comment;
    $shcut->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" );

Comment on Read and write Windows "shortcut" links
Download Code
Re: Read and write Windows "shortcut" links (cpan)
by tye (Cardinal) on Jun 09, 2003 at 15:09 UTC
Re: Read and write Windows "shortcut" links
by Anonymous Monk on Feb 22, 2013 at 08:28 UTC

      If you're curious about the binary .lnk format read Image::ExifTool::LNK

      exiftool can read .lnk files and dump the data without using Win32::OLE , works even on linux

      $ exiftool local.file.xp.test ExifTool Version Number : 9.13 File Name : local.file.xp.test Directory : D:/shellify.0.4.src/ShellifyTest/Fil +es File Size : 498 bytes File Modification Date/Time : 2010:07:24 10:19:18-07:00 File Access Date/Time : 2013:02:22 05:10:21-08:00 File Creation Date/Time : 2013:02:22 02:27:09-08:00 File Permissions : rw-rw-rw- File Type : Windows Shortcut MIME Type : application/octet-stream Flags : IDList, LinkInfo, RelativePath, Work +ingDir, Unicode File Attributes : Archive Create Date : 2004:08:19 05:16:19-07:00 Access Date : 2010:07:09 00:37:36-07:00 Modify Date : 2004:08:05 04:00:00-07:00 Target File Size : 2 Icon Index : (none) Run Window : Normal Hot Key : (none) Target File DOS Name : desktop.ini Drive Type : Fixed Disk Volume Label : SYSTEM Local Base Path : C:\WINDOWS\desktop.ini Relative Path : .\desktop.ini Working Directory : C:\WINDOWS Machine ID : al-0145

        Excellent. Thank you!

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://263858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls