-1
Function Run_Cmd(strCmd)
    Dim objShell
    Dim objScriptExec
    Dim strCmdResult

    strCmd = "%comspec% /C " + strCmd

    Set objShell = CreateObject("WScript.Shell")
    Set objScriptExec = objShell.exec(strCmd)

    set Run_Cmd = objScriptExec.StdOut

End Function

Function test ()
    '...
    Set objPropFilesList = Run_Cmd("dir /B " & sStarterDir & " | findstr /I """&test_list&"""")

    if ( objPropFilesList.count = 0 ) Then
        LogWrite "No EPM services found to verify... Aborting execution.", fAutoFixLog, bLogToConsole
        wscript.echo "No EPM services found to verify... Aborting execution."
        Exit Function
    End If


    Do Until objPropFilesList.AtEndOfStream
        '...
    Loop

End Function

In the above code,objPropFilesList returns text stream. When i have placed if condition to check the count, it skips the remaining code in this function. I doesn't understand why it skipping this code.

My suspection is that, as per below doc Count Property handles Dictionary object. The objPropFilesList returns the list of file names, does this will not be considered as dictionary object.

https://msdn.microsoft.com/en-us/library/ea5ht6ax(v=vs.84).aspx

I want to understand what exactly happening here.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • The `objPropFilesList` is a `TextStream` object which doesn't have a `Count` property, if it doesn't error and just *"skips"* then the likely cause is a stray `On Error Resume Next` line somewhere in the code not shown in the question. If `On Error Resume Next` is set somewhere in the Global scope it will then affect everything so when errors are thrown they will silently be skipped over. – user692942 Jan 27 '17 at 11:00

2 Answers2

-1

You are running a console command and capturing the output in a textstream. It is just text. You can convert to an array of lines. A = Split(A, vbcrlf).

This shows how to do file operations in VBScript. This uses recursion to give a dir /s type operation.

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")

ProcessFolder DirName

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
    Next


    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        msgbox Thing.Name & " " & Thing.path 
        ProcessFolder thing.path
    Next

End Sub
Freddie
  • 269
  • 1
  • 4
-1

To demonstrate:

  1. The error caused by accessing the non-existing .Count property was hidden by an EVIL global OERN ((c) Lankymart)
  2. You can easily use .AtEndOfStream to determine whether you need to log a failure instead of looping over the results.
  3. You really can't use Split on the TextStream.

wtf

Option Explicit

Function Run_Cmd(ByVal strCmd) ' nasty surprises without ByVal as you change strCmd in the function
'   Dim strCmdResult - not used; proves that 'long' Dim lists 'far' from the use of the variables are a bad idea
    strCmd = "%comspec% /C " + strCmd
    Set Run_Cmd = CreateObject("WScript.Shell").exec(strCmd).StdOut
End Function

Dim sPat : sPat = "vbs"
If 1 <= WScript.Arguments.Count Then sPat = WScript.Arguments(0)

Dim tsCmd : Set tsCmd = Run_Cmd("dir /B .\* | findstr /I """ & sPat & """")
WScript.Echo TypeName(tsCmd)

' OERN *hides* errors
On Error Resume Next
  tsCmd = Split(tsCmd, vbCrLf)
  WScript.Echo Err.Number, Err.Description
On Error GoTo 0

' use tsCmd.AtEndOfStream to determine whether to log before you loop over the lines
If tsCmd.AtEndOfStream Then
   WScript.Echo "Log: No EPM"
Else
    Do Until tsCmd.AtEndOfStream
      WScript.Echo tsCmd.ReadLine()
    Loop
End If

output:

cscript 41887615.vbs
TextStream
438 Das Objekt unterstützt diese Eigenschaft oder Methode nicht.
41887615.vbs

cscript 41887615.vbs pipapo
TextStream
438 Das Objekt unterstützt diese Eigenschaft oder Methode nicht.
Log: No EPM
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96