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

Does anyone know how to copy files from one directory to into another directory as shortcuts instead of actual files using a module or a function?

Thanks for any help. Brandon

Replies are listed 'Best First'.
(tye)Re: Creating shortcuts
by tye (Sage) on Oct 14, 2002 at 19:28 UTC

    See Win32::Shortcut. Unfortunately, that module includes no POD documentation so you'll probably also want to see google's take on it in order to find a nice example. Unforunately, the only good on-line documentation links that I found were all locked-out or illegal links to O'Reilly book contents. So just grab the libwin32 distribution from cpan (follow the first link I gave), unpack it, and then load Shortcut/docs/reference.html into your local browser).

            - tye (simply download PerlmonkSaintsBundle.tar.gz for more info)
      It's not nice that it doesn't show up in ActiveState's documentation tree, though the .pm file is there. A stub would be better! Just list the "obvious to use" methods with a note saying "see the Win32 docs".
Re: Creating shortcuts
by helgi (Hermit) on Oct 15, 2002 at 10:23 UTC
    The Perl built-in function 'link' does the trick on Windows. No need for a module.

    use strict; use warnings; my $file = 'C:/tmp/file1.txt'; my $link = 'C:/tmp/link_to_file1.txt'; link $file, $link or die "Cannot link $link to $file:$!\n";

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

      Have you tried this?

      perlport says

      link OLDFILE,NEWFILE

      Not implemented. (Mac OS, MPE/iX, VMS, RISC OS)

      Link count not updated because hard links are not quite that hard (They are sort of half-way between hard and soft links). (AmigaOS)

      Hard links are implemented on Win32 (Windows NT and Windows 2000) under NTFS only.

      I tried it, and after changing the original, the changes aren't reflected. Looks like it's just a plain-old copy.

      I believe NTFS supports this (cause I read http://www.hlm.inc.ru/ ), but I don't believe perl(activeperl5.6 at least) implements it.

      update: some links
      Securiteam: [NT] NTFS Hard Links Subvert Auditing
      CreateHardlink

      update: Well, after looking through my \perl directory, I do find `CreateHardlink' in perl56.dll, so I don't know what's up. I still believe the implementation must be off, cause I create a file, I create a link to it using perl, I append something to , view the file, view the link, and they're different.

      update: Whoa, after installing HardLinkMagic, and creating another link to my test file, it worked. This is very bizzare, veeeeeery bizzare.

      update: your tests run fine now. I just dragged the files/hardlinks over to notepad, and it just didn't work the first time around. This was just weird (yes I do have NTFS, i checked ;D).

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Yes, I tested it. Here is an even more through test that worked perectly on my platform, Win2K, NTFS, Activeperl 5.6.1:

        use warnings; use strict; my $file = 'C:/tmp/filename1.txt'; open IN, $file or die "Cannot open $file for reading:$!\n"; print "Contents of $file before linking:\n"; print while (<IN>); close IN; my $link = 'C:/tmp/link.txt'; if (-e $link) { unlink $link or die $!; } link $file,$link or die "Cannot link $link to $file:$!\n"; open LINK, $link or die $!; print "Contents of $link after linking:\n"; print while (<LINK>); close LINK; open (OUT, ">>", $file) or die "Cannot open $file for writing:$!\n"; print OUT "Additional content\n"; close OUT; open IN, $file or die $!; print "Coontents of $file after updating:\n"; print while (<IN>); close IN; open LINK, $link or die "Cannot open $link:$!\n"; print "Contents of $link after updating:\n"; print while (<LINK>); close LINK;
        Printed output:

        Contents of C:/tmp/filename1.txt before linking: Original contents Contents of C:/tmp/link.txt after linking: Original contents Coontents of C:/tmp/filename1.txt after updating: Original contents Additional content Contents of C:/tmp/link.txt after updating: Original contents Additional content
        As you can see, the link is maintained. YMMV on other platforms.

        --
        Regards,
        Helgi Briem
        helgi AT decode DOT is