Flylib.com

Books Software

 
 
 

Recipe4.7.Creating a Link or Junction Point


Recipe 4.7. Creating a Link or Junction Point

Problem

You want to create a link to a folder. This is sometimes referred to as a junction point. Links can be created only on NTFS file systems. Junction points are useful if you want to create a simplified path to a folder that is nested deeply in the file system.

Solution

Using a command-line interface

The linkd.exe command from the Resource Kit can create a link:

> linkd


<LinkName>




<Target>



This creates a link from folder c:\program files\perl to c:\perl :

> linkd c:\perl "c:\program files\perl"

This removes the link to perl.exe :

linkd c:\perl /d

You can also use the Sysinternals junction.exe tool to create and delete links:

> junction c:\perl "c:\program files\perl"
> junction /d c:\perl

A cool thing about junction.exe is that you can also use it to search for links:

> junction /s c:\

If you are browsing the file system with Windows Explorer, you won't be able to differentiate links from normal files and folders, but in a CMD session you can. A link shows up as <JUNCTION> , as shown here:

> dir 
 Volume in drive C is System
 Volume Serial Number is F0CE-2C6F
   
 Directory of C:\
   
01/02/2002  09:08 AM                 0 build.ini
10/06/2003  01:57 PM    <DIR>          Documents and Settings
11/02/2003  12:01 AM    <DIR>          Inetpub
11/18/2003  11:43 PM    <JUNCTION>     Perl
10/06/2003  02:14 PM    <DIR>          Program Files
11/16/2003  11:25 PM    <DIR>          scripts
12/04/2003  12:45 AM    <DIR>          WINDOWS
               6 File(s)    439,283,427 bytes
               7 Dir(s)   1,575,822,336 bytes free

Using VBScript
' This code creates a link by shelling out to the linkd command.
' ------ SCRIPT CONFIGURATION ------
strLink   = "c:\perl"
strTarget = "c:\program files\perl"
' ------ END CONFIGURATION ---------
strCommand = "linkd " & strLink & " " & strTarget
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
if intRC <> 0 then
   WScript.Echo "Error returned from running the command: " & intRC
else
   WScript.Echo "Command executed successfully"
end if

Discussion

Links, or junction points, are different from shortcuts in that they are transparent to any process or application that accesses them. A shortcut is simply a file that redirects applications to a different location. A junction point is similar to a symbolic link in Unix. When you open a junction point, applications, such as Windows Explorer, behave as if you opened the source folder. The only difference is if you delete the junction point in Windows Explorer, the source directory isn't deletedonly the junction point is deleted.

See Also

MS KB 205524 (How to create and manipulate NTFS junction points)


Recipe 4.8. Creating a Program Alias

Problem

You want to create a program alias for an application or commonly accessed file. A program alias is a little different from a shortcut or link. It is similar in function to the alias command common on most Unix platforms. The alias name can be used as an alternative to typing the full program name . For example, let's say you use the Computer Management snap-in a lot and instead of going to Start menu Administrative Tools Computer Management, you prefer to type compmgmt.msc from the Run dialog or from the command line. You could create a program alias called cmp that points to compmgmt.msc , which reduces the number of characters you have to type by nine.

Solution

The following is how you'd create the cmp alias I just described.

Create a new subkey under the following key:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

The name of the subkey should be the alias name. So you don't have to type an extension when using the alias, put .exe at the end of the name. In this case, the subkey name would be cmp.exe . You can, in fact, call the alias anything you want, but if the alias extension is not an executable extension such as .exe , you'll have to type the complete alias name when calling it. So it is perfectly fine to name the subkey cmp.abc , but I'd have to type cmp.abc instead of just cmp when typing it in the Run dialog.

Next, modify the default value under the new subkey; it shows up with the name (Default) in Registry Editor. Enter the full path to the program you are creating an alias for, which in this example would be C:\Windows\system32\compmgmt.msc . Actually, if the program is in your path, you only need to put the program name and the system will find it for you, but you are probably better off putting the complete path so there is no mistake which program you want to run.

Now you'll be able to run cmp from the Run dialog. From a command prompt, you can't just type cmp and have it launch the program. Instead you need to type start cmp , which will do the trick.

Discussion

There are a couple of things to keep in mind when entering the path to the program in the value under the subkey:

  • Don't use environment variables such %SystemRoot% . It won't work.

  • Passing parameters to the program (which would have made aliases even more useful) also doesn't seem to work.

You can force the program to start in a particular directory by creating a Path value under the alias subkey. Create a REG_SZ value entry named Path , and for its value put the full path to the directory where the program should start in.