-->

How to get target path of a junction?

2019-08-09 23:05发布

问题:

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