6

How can I change target of already existing symbolic link. Any solution would work: console, powershell,etc.

Important think is to make this change atomically so solution to delete link and then create a new one do not work for me.

Joey
  • 330,812
  • 81
  • 665
  • 668
Eugeniu Torica
  • 7,266
  • 11
  • 46
  • 62

4 Answers4

2

This seems to be possible with the ZwFsControlFile function using the FSCTL_SET_REPARSE_POINT control code. At least that's what I gleaned from how Far Manager does it via Process Monitor.

Joey
  • 330,812
  • 81
  • 665
  • 668
  • As of Mar 2017 [Far Manager](http://svn.code.sf.net/p/farmanager/code/trunk/unicode_far/flink.cpp) uses [DeviceIoControl](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx) function to change/edit either symlink or junction target(s). – Anton Krouglov Mar 01 '17 at 13:01
2

You could use transactional NTFS. See the following functions:

The downside is that MS are deprecating support for transactions. In particular transactions are not available in the new file system being introduced in Windows Server 2012.

Harry Johnston
  • 34,474
  • 6
  • 59
  • 151
  • Thank you, Harry. Never though that it is possible to have transaction. Unfortunately deprecation is a bad sign so I'm not sure I'll be able to use it. – Eugeniu Torica Jul 04 '12 at 21:18
1

You can try creating a new symbolic link, and then renaming the new link to overwrite the old.

There are some possibilities mentioned here:

Is an atomic file rename (with overwrite) possible on Windows?

Community
  • 1
  • 1
LSerni
  • 53,899
  • 9
  • 63
  • 106
-1

A slight modification of LSemi's method works for me in Windows 7 CMD console

mklink TempLink NewTarget

copy /l /y TempLink OldLink

del TempLink

I've a process that reads OldLink multiple times a second and with this method I'm able to continuously update OldLink to new targets without causing a read error. Strictly speaking, this isn't probably atomic but the time taken to effect the symlink copy must be so small, that it doesn't interfere.

Gyan
  • 74,575
  • 7
  • 138
  • 171
  • What does `/l` do? It's not listed for [Windows Server `copy`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy). – Tom Hale Aug 03 '20 at 07:43
  • 1
    Windows 7 `copy /?` shows this `/L If the source is a symbolic link, copy the link to the target instead of the actual file the source link points to.` – Gyan Aug 03 '20 at 10:53