look for a value that matches another value using VBScript

hello  Expert ,
i'm new in VBScript and i need your help ..
i have a file text  named 'test_file.txt" and i need to do :
1/ Search the position of "!MEMBERS=ENTITY" when i found it i need to
2 / search this  Label="AE0100"  when i found it i need to recuperate the value of   =>DefCurrency

in PJ my code + my file source

thanks for your help
test_file.txt
test_sc.vbs
mell lian90Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
recuperate the value of   =>DefCurrency
There appears to be multiple values for DefCurrency.
From your example, DefCurrency has the following values:


[None]
EUR
AED
AED

0
aikimarkCommented:
1. Uncomment the close of the input file
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

Open in new window

2. Add the following statement
strData = Q_29072213(strData)

Open in new window

3. Add the following code
Function Q_29072213(ByVal parmData)
    Dim strSearchFor, strSearchForCurrency
    
    Dim strData
    Dim arrData, arrHeaders, arrFields
    Dim LoopVar, DefCurrencyCol
    Dim Currencies
    
    strData = parmData
    
    strSearchFor = "!MEMBERS=ENTITY" & vbCrLf
    strSearchForCurrency = "DefCurrency"
    
    'isolate the MEMBERS=ENTITY text
    strData = Mid(strData, InStr(strData, strSearchFor) + Len(strSearchFor))
    strData = Left(strData, InStr(strData, vbCrLf & vbCrLf))
    
    arrData = Split(strData, vbCrLf)
    arrHeaders = Split(arrData(0), ";")
    For LoopVar = 0 To UBound(arrHeaders)
        If arrHeaders(LoopVar) = strSearchForCurrency Then
            DefCurrencyCol = LoopVar
            Exit For
        End If
    Next
    For LoopVar = 1 To UBound(arrData)
        arrFields = Split(arrData(LoopVar), ";")
        Currencies = Currencies & ";" & arrFields(DefCurrencyCol)
    Next
    Currencies = Mid(Currencies, 2)
    Q_29072213 = Currencies   ' or split(currencies, ";")

End Function

Open in new window

Note: you should add variable definitions for all your variables, like objFSO and objFile
1
mell lian90Author Commented:
hello ,
thanks for your answer .
to found the vaue of 'DefCurrency '  you should search "!MEMBERS=ENTITY"
and then found the value of currency when Label=X
for exemple in my source file :
I need to search
!MEMBERS=ENTITY

Open in new window

then i need to fixe a variable equal to the value of Label
in my exemple ineed to search the value of DefCurrency  when Label=AE0100
so my DefCurrency must be equal to   AED
==> so Ineed to search the value of DefCurrency  when Label=X  when i found "!MEMBERS=ENTITY"
thanks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

mell lian90Author Commented:
@aikimark ,
after some modification in your code i found the result that i search :)
thanks alot for your help:)
' Define needed I/O constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
'Dim strSearchFor
'strSearchFor ="!MEMBERS=ENTITY"
'strSearchForCurrency="AE0100"
' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No input filename specified."
   WScript.Quit
End If

' Get output file name from command line parm (if none, overwrite input file)
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
   Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
   Wscript.Quit
End If

' Read entire file contents
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close
strData = Q_29072213(strData)
Function Q_29072213(ByVal parmData)
    Dim strSearchFor, strSearchForCurrency , entity
    
    Dim strData
    Dim arrData, arrHeaders, arrFieldse,arrFields
    Dim LoopVar, DefCurrencyCol,DefLabelCol, LoopVarcu
    Dim Currencies 
    
    strData = parmData
    
    strSearchFor = "!MEMBERS=ENTITY" & vbCrLf
    strSearchForCurrency = "DefCurrency"
	strSearchForEntity="Label"
    entity="AR01"
    'isolate the MEMBERS=ENTITY text
    strData = Mid(strData, InStr(strData, strSearchFor) + Len(strSearchFor))
    strData = Left(strData, InStr(strData, vbCrLf & vbCrLf))
    
    arrData = Split(strData, vbCrLf)
    arrHeaders = Split(arrData(0), ";")
    For LoopVar = 0 To UBound(arrHeaders)
	'extract label 
	 If arrHeaders(LoopVar) = strSearchForEntity Then
	        DefLabelCol = LoopVar 
			Exit For
			End If
    For LoopVarur = 0 To UBound(arrHeaders)
     If arrHeaders(LoopVarur) = strSearchForCurrency Then
		DefCurrencyCol = LoopVarur
		    Exit For  
	End If
    Next
	Next
	For LoopVar = 1 To UBound(arrData)
	arrFieldse = Split(arrData(LoopVar), ";")
	
	 if entity = arrFieldse(DefLabelCol) then 
        
        Currencies = arrFieldse(DefCurrencyCol)
    End If
   
	 Next
      Q_29072213 = Currencies   ' or split(currencies, ";")
Wscript.Echo Currencies
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Since the author indicates (and the posted code bears out) that expert comment ID: 42394005 was the foundation of the solution, it seems like that comment should be awarded points as an assist.


»bp
0
aikimarkCommented:
You don't get to select ONLY your comment as the solution if it is based on my code.  You may include your comment in the solution mix, but you need to select my comment with the code on which you've based your solution.
0
mell lian90Author Commented:
sorry this is the first time that i close a tiket , so i don't know what i need to do or what i need to choose to close the tiket
but the only thing that i know , that  because of your help i found the solution
thanks again
0
aikimarkCommented:
search the value of DefCurrency  when Label=AE0100
There may be a more elegant solution that requires less code.
' Define needed I/O constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
'Dim strSearchFor
'strSearchFor ="!MEMBERS=ENTITY"
'strSearchForCurrency="AE0100"
' Get input file name from command line parm
If (Wscript.Arguments.Count > 0) Then
   strInFile = Wscript.Arguments(0)
Else
   Wscript.Echo "No input filename specified."
   Wscript.Quit
End If

' Get output file name from command line parm (if none, overwrite input file)
If (Wscript.Arguments.Count > 1) Then
   strOutFile = Wscript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
   Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
   Wscript.Quit
End If

' Read entire file contents
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

Wscript.Echo Currencies Q_29072213(strData)

Function Q_29072213(ByVal parmData)
    Dim oRE As Object
    Dim oMatches As Object

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = False
    oRE.Pattern = "!MEMBERS=ENTITY(?:(?:.|\n)+?)\nAE0100;([^;]+)(?:(?:.|\n)+?)\r\n\r\n"

    If oRE.test(parmData) Then
        Set oMatches = oRE.Execute(parmData)
        Q_29072213 = oMatches(0).submatches(0)
    Else
        Q_29072213 = vbNullString
    End If
    
    Set oMatches = Nothing
    Set oRE = Nothing
End Function

Open in new window

1
mell lian90Author Commented:
good solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.