Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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;

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;

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", "" );

Replies are listed 'Best First'.
Re: Read and write Windows "shortcut" links (cpan)
by tye (Sage) 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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://263858]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2017-03-30 15:42 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (360 votes). Check out past polls.