Question : VBScript: Need to only output the required result, without the junk.

I need to modify the following script to extract whatever text is located between: "<strong>Parameter One: </strong>" and "</div>" and output it in "Output.txt", without the junk (see below).

You will find a simplistic modified sample of "Input.txt" so you can get the idea. In reality, Input.txt containes a combination of HTML and JavaScript, making the Output.txt file kind of messy; and the required string is located at it's last line.

I did not include the original input.txt file because it contains lots of confidential infos and it would take me to much time to review it. So I included the modified Output.txt file. If you absolutly need the modified original input.txt file, please let me know.


Thanks,
Rene




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



THE SIMPLISTIC FAKE INPUT.TXT 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 ONLY:
**************************************************************
Elevator one is online=True



VB SCRIPT TO FIXE:
**************************************************************
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




CURRENT OUTPUT.TXT FILE CONTAINING THE JUNK:
**************************************************************
Is attached as a file for your review.
Attachments:
 
CURRENT OUTPUT.TXT FILE CONTAINING THE JUNK
 

Answer : VBScript: Need to only output the required result, without the junk.

try it please:
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:
29:
30:
31:
32:
33:
34:
dim filename,placeholder,data,tokens,subtokens,tok,startindex,index

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

startindex = InStr(data,START_TOKEN)
tokens = Split(data, START_TOKEN)

if startindex > 0 then
	startindex = 1
end if

for index = startindex to UBound(tokens)
	subtokens = Split(tokens(index), END_TOKEN)
	if UBound(subtokens) > 0 then
		if subtokens(0) <> ""  then
			WScript.Echo subtokens(0)
		end if
	end if
next
Random Solutions  
 
programming4us programming4us