0

I would like to check a data in my file exist or not in an array data that I have. It will return 1 and 0 if its exit or not. Inside my file is like this:

2j2H4F6d9d0d3hdfasgt.y7

But I cut the last 2 lines. And my array data is like this: [2w fr 5k 2j 0w]. I want to check whether my array data exist inside my file.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

XX = 0

Set wshShell = CreateObject("WScript.Shell")

strFBString = wshShell.ExpandEnvironmentStrings("%FB%")

WScript.Echo "==>"
WScript.Echo "strFBString: " & strFBString

Set wshShell = Nothing

For i = 1 To Len(strFBString) Step 2
    If StrComp(Mid(strFBString, i, 2), [2w fr 5k 2j 0w]) = 0 Then
        XX = 1
    End If
Next

WScript.Echo "XX: " & XX

WScript.Quit(XX)
0

1 Answer 1

1

For one thing, [2w fr 5k 2j 0w] is not a valid array definition in VBScript. If you want to define an array with these 5 string elements you need to do it like this:

Array("2w", "fr", "5k", "2j", "0w")

Also, StrComp() is for comparing a string to another string. It does not support comparing a string to an array. For comparing a string to each element of an array you need a loop. How to build that loop depends on the result you want to achieve, though.

Looking at your code it seems you want to find a match in 2j2H4..., but not in w2j2H..., so simply using InStr() probably won't work for you. In that case you could use an inner loop for the comparison:

ref = Array("2w", "fr", "5k", "2j", "0w")
For i = 1 To Len(strFBString) Step 2
    For Each s In ref
        If Mid(strFBString, i, 2) = s Then
            '...
        End If
    Next
Next

But like I already said, details depend on the desired end result. If you want to check if your input string contains any of the array values you could do something like this:

ref   = Array("2w", "fr", "5k", "2j", "0w")
found = False
For i = 1 To Len(strFBString) Step 2
    For Each s In ref
        If Mid(strFBString, i, 2) = s Then
            found = True
            Exit For
        End If
    Next
Next

If on the other hand you wanted to check if your input string contains all of the reference strings you'd probably do something like this instead:

ref   = Array("2w", "fr", "5k", "2j", "0w")
For Each s In ref
    found = False
    For i = 1 To Len(strFBString) Step 2
        If Mid(strFBString, i, 2) = s Then
            found = True
            Exit For
        End If
    Next
    If Not found Then Exit For
Next

You could also use an entirely different approach, like putting your data in a dictionary:

data = CreateObject("Scripting.Dictionary")
For i = 1 To Len(strFBString) Step 2
    data(Mid(strFBString, i, 2)) = True
Next

Using that approach you could check if the data contains any of the reference values like this:

found = False
For s In Array("2w", "fr", "5k", "2j", "0w")
    If data.Exists(s) Then
        found = True
        Exit For
    End If
Next

or check if the data contains all of the reference values like this:

found = True
For s In Array("2w", "fr", "5k", "2j", "0w")
    If Not data.Exists(s) Then
        found = False
        Exit For
    End If
Next
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. Its great. But my expectation is I want to compare array to array. The data inside my file is a string like this 2j2H4F6d9d0d3hdfasgt.y7 . First, I need to make it to array and each array 2 bit ("2j", "2H", "4F",.....) and my Array reference is : ("2w", "fr", "5k", "2j", "0w"). So, I want to check whether my reference array exist in my first array, If it is exist, It return me 0 and 1 for exist. Could you please give me any idea? Thanks
@SBR Please re-read my answer. Pay particular attention to the second half.
But how do I compare 2 by 2 the data inside the file? because my data inside the file is like this 2j2H4F6d9d0d3hdfasgt.y7 . And I dont want to include to compare 2 last line.
@SBR What do you mean "how"? You need to integrate the code fragments from my answer with your existing script, obviously. If you find yourself unable to do that you need to find (and work your way through) a VBScript tutorial first. I don't intend to provide you with a turnkey solution.
BTW, if you're asking how to read the input string from a file: that would be a followup question that should be asked as a new question. After you reviewed the documentation that is. Make sure to focus the new question on extracting the data from the file rather than the comparison operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.