Take the 2-minute tour ×

I have the below code and all I want to do is to check if a share called "temp" exists on a server. If it does then it needs to continue on a do some more code that I will add in.

If it does not then I want it to do is to run some other peice of code that WDS will create the share.

I know I am going wrong on this so any help would be appreciated! Currently when I run it on my computer that has a "temp" share it says the share does not exist...when it does!?

 strComputer = "." 
 Set objWMIService = GetObject("winmgmts:" _

 strShareName = "temp" 
  Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Name = '" & strShareName & "'")

 For each objShare in colShares 
 If (Err.Number <> 0) Then 
 WScript.Echo "Share Exists" 
 Wscript.Echo "Share Does not exists" 
 End If 
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You have your 'if exists' logic the wrong way around. Change:

If (Err.Number <> 0) Then


If (Err.Number = 0) Then

In this instance an Err.Number of 0 (i.e. no error) would mean the share exists.

share|improve this answer
thanks a lot for that! –  lara400 Nov 2 '11 at 22:13
add comment

Your Answer


