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

I'm creating a script that needs to operate under both 'real' OSen and (for reasons too painful to go into) Win32. One of the requirements is that it create symlinks to the latest logfiles it produces; e.g.

/var/log/silliness/ latest/ results errors $year/$month/$day/ results.$hour:$min:$sec errors.$hour:$min:$sec

Is there a module somewhere -- or (ideally) a simple code snippet -- to create a "shortcut" file like C:/var/log/silliness/latest/results.LNK (or C:\\..., whatever) so that Win32 users can use Explorer to open the latest output files just like the Unix users can from gmc (or the shell, or...)?

Replies are listed 'Best First'.
Re: How do I create a Win32 'shortcut' (.LNK) file?
by tye (Sage) on Jan 24, 2001 at 21:43 UTC

    Uh, I know the name would be hard to find, but the module is called Win32::Shortcut and is included with ActivePerl. ;~)

            - tye (but my friends call me "Tye")
Here's the code
by Foggy Bottoms (Monk) on Jun 12, 2003 at 11:03 UTC
    You've probably found it by now (it's been 2 years since you posted) but I'll write it down for others who may be looking for it :
    my $LINK=new Win32::Shortcut();
    $LINK->Set("c:\\yourprogramname",
                "list of arguments", 
                "c:\\wherever you want it to run from",
                "description",
                1, #window state (1 is normal)
                hex('55x0337'), #shorcut key*
                "",#icon file eg win32.dll
                0);#icon number in file
    $LINK->Save("c:\justaname.lnk");
    $LINK->Close();
    

    * shortcut key : it's 2 bytes followed by the key's code (for example 0337 is Q). The first 2 bytes give you the mode but I didn't find any info on the web - all I read was that no one knew how it worked. I'm still looking for that piece of info though.
    For extra information you can go to http://search.cpan.org/src/GSAR/libwin32-0.191/Shortcut/docs/reference.html
      Actually I just found out how to interpret the shorcut key code more or less :
      What I previously wrote is wrong. It always should be 0x something and not 55xsomething (I don't know what overcame - I guess I wasn't paying attention).
      0x0359 : the digits in bold give you the mode (ie CTRL+ or CTRL+ALT+ ...) and the other two give the key in itself.
      Example:
      hex('0x0659') corresponds to CTRL+ALT+Y. 06 represents CTRL+ALT and 59 represents Y...
      Here's my version:
      my $Shift = 0x0100; my $Ctrl = 0x0200; my $Alt = 0x0400; my $LINK=new Win32::Shortcut(); $LINK->Set('c:\targetfile', '', # arguements 'c:\', # directory tto execute in 'Perl Shortcut', # description 1, # window state (1 is normal) $Shift+$Ctrl+ord('S'), # shorcut key '', # icon file eg win32.dll 0); # icon number in file my $shortfile = 'C:\WINNT\Profiles\PhilHibbs\Start Menu\Programs\Acces +sories\shortcut.lnk'; $LINK->Save($shortfile); # run Explorer targetting the file just created # system("Explorer /e,/select,$shortfile"); $LINK->Close();
      Is there a better way to do constants? I'm more familiar with C and C++ where idioms differ.
        Well, the way to declare constants in perl is as follows :
             use constant NAME => value;
        You can store those constants in a separate package file of course !