(TIP) Creating symbolic links on NTFS

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
Sknerus
Junior Member
Junior Member
Posts: 8
Joined: 2004-01-21, 15:10 UTC

(TIP) Creating symbolic links on NTFS

Post by *Sknerus »

This is the simplest way to create symbolic links on NTFS filesystem (Works only for folders, not for files)
Download linkd.exe (Win2k Resource Kit, or try Google)

1) In TC, choose \\Start\Change Start Menu...
2) Add item...
"Create a junction point"
Command: C:\...(path to)...\linkd.exe
Parameters: %T%N %P%N
Shortcut: CTRL+ALT+F5

Now put the cursor on a choosen folder, and in the second panel open destination folder. Press shortcut keys and the junction is created.

And... be careful with this links, they don't behave exactly, as in ext2fs... :)
rxyzzy
Junior Member
Junior Member
Posts: 15
Joined: 2003-12-04, 20:45 UTC

Post by *rxyzzy »

I achieve the same using the sysinternals junction command (http://www.sysinternals.com/ntw2k/source/misc.shtml#junction)

Command: "c:\Program Files\Sysinternals\Cmd\JUNCTION.EXE"
Parameters: ?%T%N %P%N

Note the ? at the start of the parameters. It allows one to change the name of the virtual directory at creation time.

It is a pity that the same seemingly cannot be done on the file level.

Robert
User avatar
SanskritFritz
Power Member
Power Member
Posts: 3693
Joined: 2003-07-24, 09:25 UTC
Location: Budapest, Hungary

Post by *SanskritFritz »

It is a pity that the same seemingly cannot be done on the file level
You can!
Quote from the windows help:
fsutil hardlink create NewFilename ExistingFilename
See also:
http://www.ghisler.ch/board/viewtopic.php?t=2324&postdays=0&postorder=asc&start=0
I switched to Linux, bye and thanks for all the fish!
User avatar
Lefteous
Power Member
Power Member
Posts: 9537
Joined: 2003-02-09, 01:18 UTC
Location: Germany
Contact:

Post by *Lefteous »

2SanskritFritz

Mh, you should have learned from the quoted thread that hardlinks and junctions are not the same thing.
User avatar
SanskritFritz
Power Member
Power Member
Posts: 3693
Joined: 2003-07-24, 09:25 UTC
Location: Budapest, Hungary

Post by *SanskritFritz »

Mh, you should have learned from the quoted thread that hardlinks and junctions are not the same thing.
:oops: 8) :D
I switched to Linux, bye and thanks for all the fish!
Sknerus
Junior Member
Junior Member
Posts: 8
Joined: 2004-01-21, 15:10 UTC

Post by *Sknerus »

I should have read that topic more carefully too :oops: Although it is really useful to have this "junction point" creation under CTRL+ALT+F5. More easier than a button on button bar.
_________________

I tried this:
fsutil (or hlinks) work only on files - and create "hard" links
linkd (or junction) work only on folders - and create "symbolic" links

Are you confused now? Cause I am :)
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

I've been using both junction points and hardlinks more and more often during the last couple of months and am quite annoyed by the lack of good support by Windows Explorer, Total Commander (the recent addition of a link-icon to junction points was VERY helpfull addition) and other file managers.

Recently I've settled on using sysinternal's linkd and Anton Popov's HardLink Magic for managing links. Both programs work quite nicely but it's still bothersome to use two very different programs for something that's (from an end-user's point of view) very much alike.

So yesterday I tried to find a better solution for Total Commander in this forum and found, among others, this thread. The suggested "?%T%N %P%N" solution works quite well, but there's still the issue of using linkd for directories and HLM or Microsoft's fsutils for files. There are also some ActiveX and COM Dll's to be found, providing the appropriate interfaces to link files and directories, but never both (ie. I found no such solution).

Which is why I finally decided to try and create a better solution and wrote a Dll and an accompanying JScript myself.

Please keep in mind that fsUtilEx.dll and fslink.js were both created yesterday with some minor adjustments today, so they're not exactly tested as thouroughly as you might wish. I have however tried my best to secure the delting-parts of the code. The DeleteXXX function of fsUtilEx.dll will not delete a file if it's the last instance (ie. with a link count of 1) and won't delete a directory unless it's a junction point (ie. the directory linked to will not get deleted). Furthermore, use the /strict switch when calling fslink.js to get an additional confirmation request before deleting or replacing any file or directory.

Download the package here.



The files:

fsUtilEx.dll
Installation: regsvr32 fsUtilEx.dll
Uninstallation: regsvr32 /u fsUtilEx.dll and delete the file.
Usage: You can access the fsLink API by calling the exported functions from any program (use LoadLibrary()), rundll32 or use it's COM interfaces from a script.

fslink.js
Installation: The script uses functions defined in a file located at the path pointed to by the environment variable JSUTILS (eg. JSUTILS=c:\bin\setup_templates_utils.js). You may however choose any other path and filename, simply adjust the environment variable JSUTILS (Control Panel->System->Advanced->Environment Variables->User variables or System variables).
Uninstallation: Delete the files and remove JSUTILS.
Usage: Run the script without arguments to get a syntax description.

setup_templates_utils.js
Support functions. Not needed unless you wish to use fslink.js.

test.js
The test script I use to verify the interfaces and which shows how to call the function using rundll32, fslink.js and the COM interface. Not needed by fsUtilsEx.dll and fslink.js.



How to use the files:

by calling rundll32:

Code: Select all

rundll32 fsUtilEx,CreateLink c:\progs "c:\Program Files"
rundll32 fsUtilEx,DeleteFolderLink c:\progs
rundll32 fsUtilEx,DeleteLink c:\progs
within a JScript (VBScript very similar):

Code: Select all

var fslink = WScript.CreateObject("fsUtilEx.fsLink.1");
fslink.CreateFolderLink("c:\\temp", "c:\\WINDOWS\\temp");
fslink.DeleteFileLink("c:\\temp\\my_link");
by calling fslink.js:

Code: Select all

fslink /strict /c c:\temp c:\WINDOWS\temp
fslink /c c:\temp\file c:\WINDOWS\temp\file
fslink /strict /d c:\temp
fslink /strict /d c:\temp\file


Functions exported by fsUtilEx.dll:

Code: Select all

CreateFileLink destinationPath targetPath
CreateFolderLink destinationPath targetPath
CreateVolumeLink destinationPath targetPath
CreateLink destinationPath targetPath

DeleteFileLink path
DeleteFolderLink path
DeleteVolumeLink path
DeleteLink path
COM interface IfsLink:

Code: Select all

CreateFileLink([in] BSTR szDestinationName, [in] BSTR szTargetName);
CreateFolderLink([in] BSTR szDestinationName, [in] BSTR szTargetName);
CreateVolumeLink([in] BSTR szDestinationName, [in] BSTR szTargetName);
CreateLink([in] BSTR szDestinationName, [in] BSTR szTargetName);

DeleteFileLink([in] BSTR szName);
DeleteFolderLink([in] BSTR szName);
DeleteVolumeLink([in] BSTR szName);
DeleteLink([in] BSTR szName);

LinkCount([in] BSTR szName, [out,retval] LONG* pVal);
IsLink([in] BSTR szName, [out,retval] VARIANT_BOOL* pVal);
GetLinkTarget([in] BSTR szLinkName, [out,retval] BSTR* pszTargetName);
CreateLink creates links to files, folders and volumes depending on the target path.
DeleteLink deletes linked files, folders and volumes depending on the path.

LinkCount
- for files: Returns the number of instances of that file.
- for folders: Always returns 1.

IsLink
- for files: 1 if the link count is > 1, 0 otherwise.
- for folders: 1 if the folder is linked to another folder, 0 otherwise.

The Volume functions might not work yet.
Last edited by OliverPA on 2004-02-12, 17:54 UTC, edited 2 times in total.
User avatar
Clo
Moderator
Moderator
Posts: 5731
Joined: 2003-12-02, 19:01 UTC
Location: Bordeaux, France
Contact:

Please, move---

Post by *Clo »

2OliverPA
:D   Hello!
Welcome on board!

• Your post is quite interesting, but I guess it'ld be more appropriate to move it into the Plugin Forum...

:mrgreen: Kind regards,
Claude
Clo
#31505 Traducteur Français de TC French translator Aide en Français Tutoriels Français English Tutorials
User avatar
Black Dog
Power Member
Power Member
Posts: 1024
Joined: 2003-02-05, 22:17 UTC
Location: Odessa
Contact:

Post by *Black Dog »

[face=courier]On 24-01-2004 21:37:49 +0000 Clo wrote:

C> • Your post is quite interesting, but I guess it'ld be more
C> appropriate to move it into the Plugin Forum...


This post is exactly there it should be.[/face]
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

I posted into this thread because I figured the people that read it would know enough about links on ntfs volumes to understand the script's status/error messages and recognize when it's about to do something wrong. Someone who's completely new to linking might not understand the difference between eg. deleting links and the files they link to.
I've been using the script for about a week and it has worked as expected. Just the 'quiet' and 'overwrite' switches have a couple of minor flaws where the script would still show message boxes and ask for confirmation instead of just quitting with an error code (as expected when calling from batch files). I rewrote the handling for these switches and will update the package today or tomorrow.
The dll is also slightly updated, but the changes are minor and mostly internal, so the interfaces will behave just as before.
I will post the program (including a simple installation/deinstallation solution) to a new thread once I feel comfortable that it works well enough to be used by a novice.
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

There's a new version available at the same address.

The files fsUtilEx.dll, setup_templates_utils.js and test.js are updated, fslink.js stays the same for now since I'm not yet convinced the modified switches handling is correct.

Probably of less interest to you guys, but for completeness sake I'll mention that there is a new COM interface IfsFileId available which allows filename to file-id conversion and vice versa 1).

Code: Select all

uint64 GetFileId([in] BSTR fileName);
BSTR GetFileName([in] uint64 fileId);
BSTR GetFilePath([in] uint64 fileId);

uint64 GetVolumeId([in] BSTR VolumePath);
BSTR GetVolumeMountPoint([in] uint64 VolumeId);

VARIANT_BOOL Exists([in] uint64 fileId);

uint64 VolumeId();
void VolumeId([in] uint64 rhs);

BSTR PrintId([in] uint64 id);
uint64 ParseIdString(BSTR szId);
For samples on how to use the interface see test.js.


1) NTFS maintains an internal id for every object which stays the same even if it's renamed or moved to another directory on the same volume. Furthermore linked files (not directories) keep the same id as well which comes in very handy when eg. copying/packing files since you can check whether that file is already included.
Last edited by OliverPA on 2004-02-12, 17:57 UTC, edited 1 time in total.
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

Just finished testing the switches handling and all tests passed, so this new version could be considered quite reliable. As of now the quiet switch is really quiet (good for batch file usage), the strict switch overrides both quiet and overwrite and there's a new command /t which tests whether a file/folder is a link.

The new files (with updates to fsUtilEx.dll, fslink.js and test.js) are once again available here.

my TC configuration:
menu1=create link
cmd1=%BINDIR%/fslink.js
param1=?/strict /c %T%N %P%N
key1=5
menu2=delete link
cmd2=%BINDIR%/fslink.js
param2=?/strict /d %P%N
key2=8

You can remove '/strict' from the parameter list once you trust the script.

ps: Start fslink.js without parameters to get a usage description.
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

This is a somewhat bigger update with the following changes:

- fixed bug where a file would be opened successfully although a directory was expected
- fixed bug where CreateVolumeLink() would fail due to non-empty destination folder if it had to replace a linked folder
- eliminated necessity to set the JSUTILS environment variable
- script now first tries to load JSUTILS and if that fails search for jsutils.js or setup_templates_utils.js in the script's directory and finally in the current directory
- added /g (as in 'get') command to fslink.js which displays the link target path
- improved COM error return values for most functions (don't just return E_FAIL anymore)
- implemented GetLinkTarget() and IsLink() for volume mount points
- GetVolumeId() now also accepts Volume{...} paths
- added a few additional input tests so functions won't fail if an input path has no trailing backspace or a volume name has no \\?\ prefix
- added PrintId() and ParseIdString() to IfsFileId for languages without 64bit unsigned integer support

Download here.
OliverPA
Junior Member
Junior Member
Posts: 20
Joined: 2004-01-24, 18:04 UTC
Location: Vienna, Austria

Post by *OliverPA »

- rewrote the switches handling in fslink.js thereby fixing a few rarely occuring bugs

- added /dd switch which shows an inputbox containing the destination path
This eliminates the need for the rather messy ? in TC's commandline-argument list (eg. '? /d %P%N') and allows for much easier changing of the destination path

- added /crp command for creating reparse points without target-checks
While essentially creating the same disk-element as /c, this allows setting the target path to anything the underlying OS functions will accept, no tests are done by either fslink.js or the IfsLink implementation.
While using this feature I 'discovered' (ie. I hadn't read about it in the NTFS documentation) that you can set the target of a directory link to ANY directory on ANY other drive and not just directories on the same volume and volume mount points. The destination path may even be on a non-NTFS volume (eg cdrom).
Mind you, DeviceIOControl() (the Win32 call responsible for setting reparse points) does some error checking on the arguments (though it allows recursive linking which allegedly really confuses the system) so you shouldn't be able to create a reparse point containing parameters that could possibly corrupt the volume's data or create hierarchies that would make other programs (unaware of the existance of links) corrupt it or simply lead to unexpected results, but please always, always, delete reparse points (or any links for that matter) using the delete routines of fslink.js, fsUtilEx.dll or any other tool capable of handling links rather than deleting it with the console's functions, windows explorer, TC (!),...

edit -> Aparently TC handles reparse points correctly with 'use explorer delete method' unselected, ie when deleting the link it deletes only the link rather than the target files! I'm pretty sure to have tested this a (long) while ago and it did the same as windows explorer namely delete everything. Well, must've been before 'use explorer...' option was added.
Kudos!
<- edit

The difference between deleting reparse points correctly and handling them as though they were ordinary directories is that you can't use the OS's delete-functions which would request the deletion to eg c:\link, the OS would recognize that it's a link and return the target c:\asd instead, and delete c:\asd\* rather than c:\link
Fortunately when deleting linked files there is no such problem as the NTFS concept of a file link has no source and destination.

PS: I'm beginning to feel a bit awkward about having this monolog here as it looks like a rather desperate attempt to get some attention... even more so than this PS :wink:
PPS: That PS is not to be understood as a request for "thanks!!! you da man!!!!!" messages but rather consequence of above mentioned awkwardness :D

Oh btw, Mr Ghisler? In case you're reading this: Are you planning on implementing some kind of indication whether a file is linked or not (big thanks for the directory icon!!)? And have you noticed TC's failure to notice directory changes that involve links (ie. one has to manually refresh to have the new linked items show up or disappear)? And how about making the ALT+SHIFT+ENTER function link-aware (ie. don't count linked objects more than once)? I'd gladly implement the latter if you just gave me the required function sig. It'd be great to get eg. "123.1/34.5 M" within the size column.

Download here.
User avatar
MGyHardSoft
Junior Member
Junior Member
Posts: 2
Joined: 2009-07-08, 22:06 UTC
Location: Budapest

Post by *MGyHardSoft »

OliverPA wrote:-
PS: I'm beginning to feel a bit awkward about having this monolog here as it looks like a rather desperate attempt to get some attention... even more so than this PS :wink:
PPS: That PS is not to be understood as a request for "thanks!!! you da man!!!!!" messages but rather consequence of above mentioned awkwardness :D
:-) However, thanks a lot for your volunteer work you have just helped me!
Rgrds: M. Gy.

#13171
Post Reply