1

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?

0

2 Answers 2

2

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
  • Thank You. This one works as promised. also helped me look into fsutil.
    – cryodream
    Jun 4, 2015 at 6:50
  • After googling, I read the answers in social.msdn.microsoft.com and visualbasicscript.com first and this one is the best! Why I didn't look here first? :-) Mar 14, 2017 at 15:16
1

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
3
  • Thank you. Works like a charm.
    – cryodream
    Jun 3, 2015 at 17:42
  • Actually, no, it doesn't. The token numbers are off (the name comes in the 4th column, not the 5th), and it breaks whenever the junction or the target contain spaces or the directory contains other folders with a name containing the substring test. Jun 3, 2015 at 19:18
  • Yes, I was so in a rush, that I did not test other use cases. It does not work.
    – cryodream
    Jun 4, 2015 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.