Which "kind" of "symbolic link"? ;-)
Oh, and read the symbolic link Wikipedia article above for the mklink
command ;-) Back-ticks (or system) can be a good friend, but note:
The default security settings in Windows Vista/Windows 7 disallow non-elevated administrators and all non-administrators from creating symbolic links. This behavior can be changed [by a security policy setting]....
Happy coding.
The WinAPI CreateSymbolicLink function might be useable directly; I am not sure if it "suffers" from the same restriction as a mklink
command above. However, this thread indicates it is still in effect.
FWIW, this "works" in Strawberry Perl 5.12. YMMV, I just typed this up and have never used it otherwise :-)
use Win32::API;
$fn = Win32::API->new(
# Note "A" function, IDK how to use Unicdoe
"kernel32", "BOOLEAN CreateSymbolicLinkA(LPTSTR lpSymlinkFileName, LPTSTR lpTargetFileName, DWORD flags)"
);
unlink("src.txt");
unlink("lnk.txt");
open(FH,">src.txt") or die $!;
close(FH);
print "src.txt exists? " , (-f "src.txt"), "\n";
print "lnk.txt exists? " , (-f "lnk.txt"), "\n";
$hr = $fn->Call("lnk.txt", "src.txt", 0);
print "Result: ", $hr, "\n";
print "lnk.txt exists? ", (-f "lnk.txt"), "\n";
open(FH,">>src.txt") or die $!;
print FH "hello world!\n";
close(FH);
open(FH,"<lnk.txt") or die $!;
print "linked data: ", scalar(<FH>), "\n";
close(FH);
My results (ran as "Administrator" -- may not work for "other users" -- I dunno why but my cmd.exe is always opening with elevated privileges):
src.txt exists? 1
lnk.txt exists?
Result:
lnk.txt exists? 1
linked data: hello world!
Directory listing:
10/22/2011 02:53 PM <DIR> .
10/22/2011 02:53 PM <DIR> ..
10/22/2011 02:54 PM 636 foo.pl
10/22/2011 02:53 PM <SYMLINK> lnk.txt [src.txt]
10/22/2011 02:53 PM 14 src.txt
I have no idea what [subtle] differences there may be, if any, between NTFS symbolic links and "UNIX" symbolic links. Also, the above won't work pre-Vista/2008 -- previous versions of NTFS do not support symbolic links (and previous versions of windows do not have the CreateSymbolicLink
function).
windows symlink perl
returns lots and lots of information about this. The short answer is that there's no such thing as a "symlink" like there is in *nix in windows. The perl docs tell you that if the underlying OS doesn't support symlinks,symlink
will fail. – Hom