Question : VB Script: Output specific text from text file

Hi there,

I need to modify the following script to extract whatever text is located between: "<strong>Parameter One: </strong>" and "</div>".


SO BY USING THE COMMAND LINE:
**************************************************************
cscript GetParameter.vbs //NOLOGO "input.txt" "Parameter 1" >output.txt


FROM THE FOLLOWING INPUT.TXT TEXT FILE:
**************************************************************
whatever line
whatever line
This is a test> and I mean it/ and why not> <strong>Parameter 1: </strong>Elevator one is online=True</div>
whatever line
whatever line


THE PARSED OUTPUT.TXT RESULT MUST BE:
**************************************************************
Elevator one is online=True




SCRIPT TO MODIFY:
**************************************************************
' Make sure the input file name is specified on the command line and get it
If (WScript.Arguments.Count > 0) Then
  filename = WScript.Arguments(0)
Else
  WScript.Echo "No input filename specified."
  WScript.Quit
End If
 
' Read entire input file into a single string variable
Set fso = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile(filename)
data = input.ReadAll
input.Close
Set input = Nothing
 
' Create dictionary object to ellininate duplicates
Set dict = CreateObject("Scripting.Dictionary")
 
' Create regular expression template to locae the order numbers
Set re = New RegExp
re.Pattern = "OrderNum=[0-9][0-9][0-9][0-9][0-9][0-9][0-9]&"
re.IgnoreCase = True
re.Global = True
 
' Loop through each match, grap order number, and if not already found add to dictionary
for Each hit In re.Execute(data)
   order=Mid(hit.Value, 10, 7)
   If Not dict.Exists(order) Then
      dict.Add order, 0
   End If
Next
 
' Output list of unique order numbers now
For Each o In dict
   Wscript.Echo o
Next



THIS SCRIPT TO BE MODIFIED IS CURRENTLY CALLED BY THE FOLLOWING COMMAND LINE:
**************************************************************
cscript GetNumbers.vbs //NOLOGO input.txt >output.txt



Thanks for your help,
Rene

Answer : VB Script: Output specific text from text file

give it a go
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
dim filename,placeholder,data,tokens,subtokens,tok

If (WScript.Arguments.Count = 2) Then
  filename = WScript.Arguments(0)
  placeholder = WScript.Arguments(1)
Else
  WScript.Echo "Not enugh arguments: expected 2 (filename + placeholder)."
  WScript.Quit
End If
 
START_TOKEN = "<strong>" & placeholder & "</strong>"
END_TOKEN = "</div>"

Set fso = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile(filename)
data = input.ReadAll
input.Close
Set input = Nothing

tokens = Split(data, START_TOKEN)
for each tok in tokens
	subtokens = Split(tok, END_TOKEN)
	if UBound(subtokens) > 0 then
		if subtokens(0) <> "" then
			WScript.Echo subtokens(0)
		end if
	end if
next
Random Solutions  
 
programming4us programming4us