Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script says Folder Exists even though it doesn't 2

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I am writing a script to check the existence of a folder.

After declaring Option Explicit and using the command-line

Code:
cscript //x script.vbs

I still cannot figure out why the script says that a folder exists even though I can see it does not

This is the snippit of code

Code:
     ' Can we connect to WMI?
      On Error Resume Next

      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
      
      If (Err.Number <> 0) Then
        HandleError()
        objErrorFile.WriteLine("Server " & strIP & ", Resolved Name: " & strName & " cannot be read")
      Else 
        Set colFolders = objWMIService.ExecQuery _
        ("Select * from Win32_Directory where Name = " & strPath)

        If colFolders.Count > 0 Then
          objFile.WriteLine(strIP & "," & strName & "," & "YES")
        Else
          objFile.WriteLine(strIP & "," & strName & "," & "NO")
 	End If
       
      End If


And just in case, below is the code in it's entirety

Code:
Option Explicit


''''''''''Declare Variables''''''''''

Dim objShell, objScriptExec, objStdOut
Dim objInputFSO, objOutputFSO, objFile, objErrorOutputFSO, objErrorFile
Dim strInputFile, strData, arrLines, strLine, strIP, strName
Dim strCommand, strPingResult, bNoPing, arrayPingResult, strCheck, arrayPCLine
Dim objReg, strKeyPath, strValueName, strValue, strPath
Dim objWMIService, colFolders, strDrive, strArray, strRel, strGUP, strFolder

''''''''''Declare Constants''''''''''

const ForAppending = 8
const strOutputFile = "output_server_gup_status.csv"
const strErrOutputFile = "error_log_server_gup_status.txt"
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002



''''''''''Declare Sub Routines''''''''''

Sub HandleError()
     On Error Goto 0
     Err.Clear 
     bErrorFound = True
End Sub


''''''''''Declare Functions''''''''''

Function Ping(strIP)   

  
  bNoPing = False
  Set objShell = CreateObject("WScript.Shell")
  strCommand = "ping.exe -n 1 -a " & strIP
  Set objScriptExec = objShell.Exec(strCommand)
  strPingResult = objScriptExec.StdOut.ReadAll
  Set objStdOut = objScriptExec.StdOut



  If InStr(strPingResult, "Reply from") Then
    arrayPingResult = split(strPingResult, vbcrlf)
    arrayPCLine = split(arrayPingResult(1), " ")
    strName = arrayPCLine(1)
  Else 
    strName = "NULL"
    objErrorFile.WriteLine(strPingResult)
    objErrorFile.WriteLine(vbcrlf)
    bNoPing = True
  End If

End Function


''''''''''Begin Execution''''''''''


'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")

'Name of the input text file
strInputFile = InputBox("What is the name of file with list of IP Addresses?")

'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Create an Output File System Object
Set objOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objFile = objOutputFSO.CreateTextFile(strOutputFile)

'Create an Output File System Object for Recording Error
Set objErrorOutputFSO = CreateObject("Scripting.FileSystemObject")

'Create Output File
Set objErrorFile = objOutputFSO.CreateTextFile(strErrOutputFile)

objFile.WriteLine("IP Address,Computer, Is Server a GUP, Last Time Server Acted as GUP?")

'Step through the lines
For Each strLine in arrLines

  strIP = strLine
  Ping(strIP)

  '*****If we can ping computer*****
  If bNoPing = False Then


    ' Can we connect to registry?
    On Error Resume Next

    Set objReg=GetObject( _
   "winmgmts:\\" &_
    strIP & "\root\default:StdRegProv")

    If (Err.Number <> 0) Then
      HandleError()
      objErrorFile.WriteLine("Cannot connect to registry of " & strIP & ", Resolved Name: " & strName)
    Else
     'Find the SEP Installation path
      strKeyPath = "SOFTWARE\Symantec\Symantec Endpoint Protection\AV"
      strValueName = "Home Directory"
      objReg.GetStringValue _
       HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
      strPath = strValue & "SharedUpdates"

      
     ' Can we connect to WMI?
      On Error Resume Next

      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
      
      If (Err.Number <> 0) Then
        HandleError()
        objErrorFile.WriteLine("Server " & strIP & ", Resolved Name: " & strName & " cannot be read")
      Else 
        Set colFolders = objWMIService.ExecQuery _
        ("Select * from Win32_Directory where Name = " & strPath)

        If colFolders.Count > 0 Then
          objFile.WriteLine(strIP & "," & strName & "," & "YES")
        Else
          objFile.WriteLine(strIP & "," & strName & "," & "NO")
 	End If
       
      End If


    End If

    
  End If

  
Next

  Wscript.echo "Output is located at output_server_gup_status.csv"
  Wscript.echo "Error log is located at error_log_server_gup_status.txt"

'Cleanup
Set objInputFSO=Nothing
Set objOutputFSO=Nothing
 
it probably doesn't. my guess is that there is some sort of error, [tt]on error resume next[/tt] is ignoring/covering up the error and the script says the folder exists. remove on error resume next and see if any errors occur.

my experience with OERN (on error resume next) is that if an error occurs, OERN will execute the next command in its scope.

Code:
on error resume next
command A
   command B
   command B2
      command C
      [red]an error has occurred[/red]
      command C2
   command B3
command A2
the next command to be executed is not C2, but A2 because that is the next command in the scope of OERN. Be careful with OERN, it can lead to wasted time chasing a problem that might not even exist!

-Geates


-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Replace this:
If colFolders.Count > 0 Then
objFile.WriteLine(strIP & "," & strName & "," & "YES")
Else
objFile.WriteLine(strIP & "," & strName & "," & "NO")
End If
with this:
If colFolders.Count = 0 Or Err.Number <> 0 Then
objFile.WriteLine(strIP & "," & strName & "," & "NO")
Else
objFile.WriteLine(strIP & "," & strName & "," & "YES")
End If

@Geates:
For me, OERN will execute the next instruction in source sequence, eg:
If AnyFalseConditionButGeneratingAnError Then
Instruction is executed

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
>For me, OERN will execute the next instruction in source sequence

And for me
 
@ PHV

I replaced the code such that

Code:
	If colFolders.Count = 0 Or Err.Number <> 0 Then
  	  objFile.WriteLine(strIP & "," & strName & "," & "NO")
	Else
  	  objFile.WriteLine(strIP & "," & strName & "," & "YES")
	End If

And now, whether the folder exists or now

Code:
objFile.WriteLine(strIP & "," & strName & "," & "NO")

always executes.

I tried using AND instead of Or

Code:
If colFolders.Count = 0 AND Err.Number <> 0 Then

and still,

Code:
objFile.WriteLine(strIP & "," & strName & "," & "NO")

always executes.

Please guide.
 
@ Geats

So far, the following seems to work. It doesn't use error checking:

Code:
     'Get the drive letter and relative path of Directory strPath
     strArray = Split(strPath,":",-1,1)
     strDrive = strArray(0)
     strRel = strArray(1)

     Set objFSO = CreateObject("Scripting.FileSystemObject") 

     If objFSO.FolderExists("\\" & strIP & "\" & strDrive & "$" & strRel) Then
        wscript.echo "\\" & strIP & "\" & strDrive & "$" & strRel & " EXISTS" 
        objFile.WriteLine(strIP & "," & strName & "," & "YES") 
     Else
        wscript.echo "\\" & strIP & "\" & strDrive & "$" & strRel & " DOES NOT EXIST"
        objFile.WriteLine(strIP & "," & strName & "," & "NO") 
     End If
 
@JustScriptIt
I like to use UNC paths to acces remote folders also.

I wonder if objWMIService ever equaled the local WMI. If that were the case and the path didn't exist on the local machine, then that would be the "error" without being an error at all. I would verify the value of strIP right after it is set. It's possible you were looking at the file system of the remote machine while the objWMIService was refering to the local machine.

Also, your ping function is a function but you are using it as a sub. A function returns a value. I would recommend making use of this and, at the same time, eliminate a need for a global var and improve readability.

Code:
function ping(strIP)
   'set the function to false
   ping = false

   if InStr(strPingResult, "Reply from") Then
      ping = true
   end if
end function


strIP = "4.2.2.2"
if ping(strIP) then
   'it pings
else
   'it doesn't ping
end if

@all. Yes, the same is true for me also. However, when subs and functions are introduced, what I said is true. This was the scope to which I was referring but failed to specify.

Code:
on error resume next

sub sub1
	if (1 / 0) then
		msgbox "A"
	else
		msgbox "B"
	end if
end sub

sub sub2
	msgbox "C"
end sub

sub1
sub2

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
@JustScriptIt

When using WMI to connect remote file system, you have to use \\ intsted of Eg: C:\\mainfolder\\subfolder.

Below code works for me, when I connect to remote fie system.

Set ObjWMI = GetObject("WinMgmts:\\" & StrIP & "\root\CIMV2")
Set ColItem = ObjWMI.ExecQuery("SELECT * FROM WIN32_Directory WHERE Name='C:\\Programme Files\\Symantec\\AV'")

If you are getting the path from registry, you may have to replace '\' with '\\' before using in wmi query.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top