How to get target path of a junction?

不想你离开。 提交于 2019-12-11 12:29:50

问题


I have a folder C:\the Junction\test, which is actually a junction, and the real path (target) is D:\the real\path\to\the folder.

How can I find out that real target path in VBScript?


回答1:


I'm not aware of a way to get this information with plain VBScript, but you can shell out to fsutil to extract this information:

foldername = "C:\the Junction\test"

Set sh = CreateObject("WScript.Shell")
Set fsutil = sh.Exec("fsutil reparsepoint query """ & foldername & """")

Do While fsutil.Status = 0
  WScript.Sleep 100
Loop

If fsutil.ExitCode <> 0 Then
  WScript.Echo "An error occurred (" & fsutil.ExitCode & ")."
  WScript.Quit fsutil.ExitCode
End If

Set re = New RegExp
re.Pattern = "Substitute Name:\s+(.*)"

For Each m In re.Execute(fsutil.StdOut.ReadAll)
  targetPath = m.SubMatches(0)
Next

WScript.Echo targetPath

Change the pattern to Substitute Name:\s+\\\?\?\\(.*) if you want to exclude the leading \??\ from the path.




回答2:


Give a try this code:

Set p = CreateObject("WScript.Shell").Exec("cmd /c @echo off & for /f ""tokens=5,6"" %a IN ('dir ""c:\the junction"" /a:d ^|find ""test""') do echo The real path of ""%a"" is %b")
Do While p.Status = 0
  WScript.Sleep 100
Loop
WScript.Echo p.StdOut.ReadAll


来源:https://stackoverflow.com/questions/30624989/how-to-get-target-path-of-a-junction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!