Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have set of MAP in text file as below.

map alias elr72_511
type regular byRule
roles replace admin to owner_roles
comment Converted_from_elr72_rule_31
to 1/2/x29,1/3/x27
from 1/4/x5..x6
exit
map alias elr72_582
type regular byRule
roles replace admin to owner_roles
comment Converted_from_elr72_rule_274
to 1/2/x29,1/3/x27
from 1/4/x5..x6
exit

from that text file i need to find all maps name which are going to 1/2/x29.

What I have tried:

Dim objFSO, filepath, objInputFile, tmpStr, substrToFind

Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\Users\Desktop\New Text Document.txt"
substrToFind = "1/2/x27"
Set objInputFile = objFSO.OpenTextFile(filepath)
tmpStr = objInputFile.Read
If InStr(tmpStr, substrToFind) <= 0 Then
WScript.Echo "No matches"
Else
WScript.Echo "Found match"
End If
Posted
Updated 1-May-17 0:01am

1 solution

There are two simple ways to solve this:
1) if it is correct that each map ends with the key word 'exit' and if this key word won't occur in other positions within a map, you may read the complete file content into a single string, then use the split method to turn the string into an array using 'exit' as the separator. With a For-Next-Loop you can check each array element for the searched string.

2) you may use a regex object to parse the file content - this would be more elegant and easier but requires a good understanding of regular expressions.

Here's a sample created with VBScript:
VB
Dim objFSO, filepath, objInputFile, tmpStr, substrToFind, arrRecords, r
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\Users\Desktop\New Text Document.txt"
substrToFind = "1/2/x29,1/3/x27"
Set objInputFile = objFSO.OpenTextFile(filepath, ForReading)
tmpStr = objInputFile.ReadAll()


If InStr(tmpStr, substrToFind) <= 0 Then
	WScript.Echo "No matches"
Else
	arrRecords = Split(tmpStr, "exit")
	
	For r = LBound(arrRecords, 1) To UBound(arrRecords, 1)
		If Instr(arrRecords(r), substrToFind) > 0 Then
			MsgBox "'" & substrToFind & "' found in " & (r +1) & ". record"
		End If
	Next
	
	MsgBox "No more matches found"
End If
 
Share this answer
 
v2
Comments
Member 13163682 3-May-17 8:19am    
could you share code according to your 1st suggestion, i tried with split but no luck.

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