0

The following DOS commands create a junction (called Source) pointing to a folder (called Destination) which no longer exists:

mkdir Destination
mklink /J Source Destination
rd Destination

I'm currently using the following VBScript to verify that the destination of a junction point exists:

' FileSystemObject is used for multiple things, so defined globally
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")

Function Valid_Junction(folderName)

    Valid_Junction = True

    On Error Resume Next
    Dim count : count = fso.GetFolder(folderName).Files.Count
    ' An error will be thrown if the destination doesn't exist
    If Err.Number <> 0 Then Valid_Junction = False
    On Error Goto 0

End Function

In the example above, calling Valid_Junction("Source") correctly returns False because Destination no longer exists.

Is there an easier, cleaner or more efficient way of doing this?

Richard
  • 1,471
  • 7
  • 23
  • 47
  • That is probably the simplest approach in VBScript. If you want to avoid handling errors you'll have to parse the junction target out of `dir` command output and check its existence, or switch to PowerShell. [Related](https://stackoverflow.com/q/10188485/1630171). – Ansgar Wiechers Feb 05 '19 at 20:45
  • @AnsgarWiechers Although I'm not a fan of deliberately throwing an error (to then trap it), I have to admit that I dislike shelling out to another command (to parse its output) slightly more :) – Richard Feb 08 '19 at 11:24

0 Answers0