Change target for symbolic link in windows
Asked Answered
A

4

8

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.

Ashbaugh answered 2/7, 2012 at 15:17 Comment(4)
If you wrap the deletion and recreation in a transaction I believe it will be atomic.Hoogh
What kind of transaction? File system operation like move file on the same disk I think are atomic. Two file operation are not. Correct me please if I'm wrong.Ashbaugh
See my answer. Support for transactional NTFS was added in Windows Vista.Hoogh
The answer from @Gyan below absolutely works. I also ran tests and it works for me.Anyways
C
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.

Casavant answered 2/7, 2012 at 15:27 Comment(1)
As of Mar 2017 Far Manager uses DeviceIoControl function to change/edit either symlink or junction target(s).Osteomalacia
H
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.

Hoogh answered 3/7, 2012 at 20:38 Comment(1)
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.Ashbaugh
M
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?

Mcglynn answered 2/7, 2012 at 15:29 Comment(0)
B
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.

Bioastronautics answered 13/11, 2018 at 12:14 Comment(3)
What does /l do? It's not listed for Windows Server copy.Gerianne
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.Bioastronautics
This answer is brilliant. Why was it downvoted? If anything, @Gyan, you can add more details, like output of dir to show exactly how the file system changes after each step. I tested your steps: In a separate Cygwin window, I run a simple infinite loop: while true; do cat symlink.txt; done. Then I used your trick to update symlink.txt to point to different file. Never once did my infinite read file loop fail. Works perfectly. I wonder how copy executes this operation! It is annoying that MKLINK does not have a /F (force) option to do the same. UNIX has it.Anyways

© 2022 - 2024 — McMap. All rights reserved.