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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
cryodream
  • 311
  • 1
  • 4
  • 12

2 Answers2

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank You. This one works as promised. also helped me look into fsutil. – cryodream Jun 04 '15 at 06: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? :-) – Mayra Delgado Mar 14 '17 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
Pupa Rebbe
  • 528
  • 2
  • 13
  • Thank you. Works like a charm. – cryodream Jun 03 '15 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`. – Ansgar Wiechers Jun 03 '15 at 19:18
  • Yes, I was so in a rush, that I did not test other use cases. It does not work. – cryodream Jun 04 '15 at 05:25