views:

382

answers:

2

I have a Perl script that needs to delete a directory with all its contents. Sometimes this directory contains a junction point into another directory. If I rmtree() naively, the rmtree() call will also delete all the files inside the target folder of the junction. I'm looking for a way to not do that, and instead just remove the junction.

Non Perl solutions would also be appreciated.

A: 

I just typed "junction point" into Google and found my way to http://en.wikipedia.org/wiki/NTFS_junction_point

Command Prompt (cmd.exe)

  • The dir command in Windows 2000 or later recognizes junction points, displaying instead of in directory listings (use dir with the /A or /AL command-line switch).
  • Any commands that would normally affect files inside a normal directory will act the same here. Thus the command del myjunction should not be used — this will just delete all the files in the targeted directory.
  • The commands rmdir and move work fine with junctions, with the caveat that move won't let the junction move to another volume (as opposed to Windows Explorer, as mentioned above.)
  • The rmdir command is safe in that it only deletes the junction point, not the targeted files. Whilst walking through the directory with the command line interface, files can be deleted, but unlike explorer, directories can also be deleted (using rmdir /s dirname for example.)
  • Using the linkd command with the /d switch is a safe way to delete junction points.

From what I can see you can, for example, use dir and grep the output for <JUNCTION> or use the Windows rmdir. I think you can use either of these from Perl via system.

Kinopiko
A: 

To find out where are the reparse points (or "junction points", if you will):

dir /a:l /b > myjunctions.txt

Will show all reparse points in the current directory. You can add /s, but beware that reparse points inside reparse points will be listed as well.

Suppose myjunctions.txt contains the line x:\subdir\foo. To remove it, you issue

fsutil reparsepoint "x:\subdir\foo"

And voilá! Your junction point is gone, and the original directory is untouched!

Fernando Colombo