Path normalization with respect to NTFS reparse po

2019-04-14 07:23发布

Unfortunately, I'm on Windows and I need POSIX realpath.

Suppose that there's a dirpath variable in a CMD batch script:

FOODIR=D:/tmp/foo/bar/quux

where one or more middle components of the dirpath are symlinks (directory junctions in Microsoft parlance):

[/cygdrive/d/tmp]$ cmd /C dir /Ad /l
 ...
 Directory of D:\tmp
...
2014-12-10  16:44    <SYMLINKD>     foo [D:\storage\foo.canonical]

How to write a function which will transform %FOODIR% into D:/storage/foo.canonical/bar/quux?

Any solutions welcome.

1条回答
再贱就再见
2楼-- · 2019-04-14 08:11

GetFinalPathNameByHandle worked for me.

Alternatively with Powershell 5+ you may traverse path up and resolve each folder. Check for .LinkType in ('Junction', 'SymbolicLink') and use .Target[0] with optional resolution for relative symlinks.

查看更多
登录 后发表回答