tags:

views:

660

answers:

3

I'm writing a script to tidy up a bunch of media spread across my hard drives, and it works pretty well so far at home (on my Mac) as I use symlinks in a directory to give the impression that everything is organised in one location, whilst the actual data is spread across the 4 drives.

Unfortunately, I have to use Windows at work, and of course there's no symlink support there until PHP 5.3 (and I assume that requires Vista as that's when the command-line tool "mklink" first appeared).

As a workaround, I considered creating a shortcut, but I can't find a way of doing it. Is it possible, or is there a better solution I've not considered?

+1  A: 

There is support for junction points (similar to UNIX symlinks) before Vista.

You need the linkd tool from the windows resource kit (free download).

Shortcuts are just files. You can create shortcut files using the COM WScript API. The sample code does this using Python. If there exists a library for PHP that lets you do COM interop, you should be able to do something similar.

import win32com.client
import winshell

userDesktop = winshell.desktop()
shell = win32com.client.Dispatch('WScript.Shell')

shortcut = shell.CreateShortCut(userDesktop + '\\Zimbra Webmail.lnk')
shortcut.Targetpath = r'C:\Program Files\Mozilla Firefox\firefox.exe'
shortcut.Arguments = 'http://mysite.com/auth/preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox'
shortcut.save()
codeape
A: 

Thanks to the above answer, I found that you can indeed call COM from php, here's my first draft of a symlink() replacement:

if (! function_exists('symlink')) {
    function symlink($target, $link) {
        if (! substr($link, -4, '.lnk'))
            $link .= '.lnk';

        $shell = new COM('WScript.Shell');
        $shortcut = $shell->createshortcut($link);
        $shortcut->targetpath = $target;
        $shortcut->save();
    }
}
Drarok
Did you look into using the linkd command?
codeape
I did have a look, yes. But I wanted a solution that "Just Works" out of the box, with no extra tools required. And I've heard a few (potentially untrue) tales of woe out of using junction points on XP.
Drarok
Oh, also, I read this page: http://support.microsoft.com/kb/205524 which seems to suggest that junction points are for directories only, I need to point at files.
Drarok
A: 

For the record, junction points on NTFS are indeed only for directories. I have had great success using linkd.exe to create a virtual filesystem, but there are a few things to be aware of:

  • You cannot link between volumes (i.e. trying to create a link in C: that points to a location in D: won't work)
  • Unlinking things seems to sporadically delete data at the destination. Backup/Move you data before you remove a link!
  • Junction points suffer the same problems when sharing data over the network as mounted volumes - sometimes they simply don't work remotely. They also seem to randomly (i.e. sometimes) inherit some of the share permissions from the source folder, no matter how you configure the destination link.

Cheers for the useful posts about creating .lnk files, just what I was after...

DaveRandom