Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:


I have a file like this : 7G8h6Tghstwt6Y.y8 . Then, I have a string data like this : "2w" & "fr" & "7G" I am going to check whether my string data exist in the file or not.


What I have tried:

<pre lang="vb"><pre>

filepath = "C:\Users\Desktop\TS.txt"
substrToFind = "2w" & "fr" & "6J"

For i = 1 To Len(tmpStr) Step 2
    If InStr(tmpStr, substrToFind) <= 0 Then
        WScript.Echo "0:"
    Else
        WScript.Echo "1"
    End If

Next
Posted
Updated 21-Feb-19 16:54pm
Comments
OriginalGriff 20-Feb-19 2:51am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So tell us what happens that you didn't expect, or didn't happen that you did. Tell us what you did to make it happen, what you tried to find out why, where you are stuck, when help you need.
At the moment, all we have is a vague description and a tiny bit of code that does nothing too obvious.

Use the "Improve question" widget to edit your question and provide better information.

Well it's always a good idea to actually open the file first!

Dim objFile, tmpStr, objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("C:\Users\Desktop\TS.txt", ForReading)
Then you can read it line by line if your really want to...
Do While Not objFile.AtEndOfStream
   tmpStr = objFile.readline
   ' Do your checks in here
Loop
objFile.Close
Or you could just read in the entire file
tmpStr = objFile.ReadAll
Your next problem is the stuff you are trying to find substrToFind = "2w" & "fr" & "6J" will actually result in you trying to find the string
"2wfr6J"
- I don't think that is really what you mean. You should search for 2w, fr and 6J separately.
Dim b1, b2, b3
b1 = False : b2 = False : b3 = False
Const vbTextCompare = 1
If Instr(1, tmpStr, "2w", vbTextCompare) > 0 Then
    b1 = True
End If
If Instr(1, tmpStr, "fr", vbTextCompare) > 0 Then
    b2 = True
End If
If Instr(1, tmpStr, "6J", vbTextCompare) > 0 Then
    b3 = True
End If
If b1 And b2 And b3 Then
    WScript.Echo "Found"
End If
Also note that I've made the search do a text search - the default for Instr is binary.

Finally, here is some reference material - always a good place to start
VBScript - OpenTextFile Method[^]
VBScript - ReadAll Method[^]
VBScript - InStr Function[^]
 
Share this answer
 
v2
Comments
Member 14156312 21-Feb-19 2:04am    
Thanks, great solution. But I still get error name redifined vbTextCompare
CHill60 21-Feb-19 2:12am    
Then remove the line Const vbTextCompare = 1You must already have it defined somewhere (or it's predefined for you... it's been a long time since I used vbScript)
Member 14156312 21-Feb-19 3:14am    
But It not work. with this code below
Member 14156312 21-Feb-19 2:24am    
But It look not work
Dim b1, b2, b3
Dim objFile, tmpStr, objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("C:\Users\TT.txt", ForReading)

tmpStr = objFile.ReadAll
WScript.Echo tmpStr

b1 = False : b2 = False : b3 = False
If Instr(1, tmpStr, "2w") > 0 Then
b1 = True
End If
If Instr(1, tmpStr, "fr") > 0 Then
b2 = True
End If
If Instr(1, tmpStr, "6J") > 0 Then
b1 = True
End If
If b1 And b2 And b3 Then

WScript.Echo "Found"
End If
CHill60 21-Feb-19 5:07am    
I've updated my solution - I hadn't set a value for b3 (didn't edit it properly after copy+paste). Where it said
If Instr(1, tmpStr, "6J") > 0 Then
b1 = True
End If
that should have read
If Instr(1, tmpStr, "6J") > 0 Then
b3 = True
End If
This is working. But I can not figure out to print the result if the string is not found. Any Idea? Help me please

Dim fb1, fb2, fb3
Dim objFile, FBString, objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("C:\Users\fitri\Desktop\Project 2\ScanFB\FS_Data\FS_TST.txt", ForReading)

FBString = objFile.ReadAll
WScript.Echo "Feature_Byte : "  & FBString

fb1 = False : fb2 = False : fb3 = False
If Instr(1, FBString, "6J") > 0 Then
	fb1 = True
	WScript.Echo "Found 6J"
End If

If Instr(1, FBString, "fr") > 0 Then
	fb3 = True
	WScript.Echo "Found fr"
End If

If Instr(1, FBString, "jC") > 0 Then
	fb3 = True
	WScript.Echo "Found jC"
End If
 
Share this answer
 
v2
Comments
CHill60 22-Feb-19 1:38am    
Your boolean values are being assigned incorrectly and are pointless because you never use them. If you want to print "not found" you could check the booleans (when you've fixed the problem" or use "else"
CHill60 25-Feb-19 4:26am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900