Search code examples
vbscriptjunction

How to get target path of a junction?


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?


Solution

  • 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.