computing
  • 4

Solved Vbscript To Search a Text File For a Particular String

  • 4

SIR,

I have a file C:\junk\ data.txt which contain entries various entries like ie…

fuiarharjf 547253265 123456789 0987654321 ruirgsvukgof ruo2ruh2 ru24ruwfjkfjk..

I would like to have a vbscript that searches for the line that begin with both numbers 123 & 098 and write these lines to a file C:\junk\report.txt

ie… fuiarharjf 547253265 123xxxxxx 098xxxxxxx ruirgsvukgof ruo2ruh2 ru24ruwfjkfjk

and not this line ie…
fuiarharjf 547253265 222456789 2987654321 ruirgsvukgof ruo2ruh2 ru24ruwfjkfjk

this data.txt file may contain several hundreds of lines alike

please help….

thanx in advance…

Share

1 Answer

  1. I’ve made a couple of assumptions (which always comes back to bite me in the As_: your text file is crlf delimited lines, and you want all instances reported, not just one per line, and your output format is simply one instance per line.
    With that understood, this seemed to work with limited testing:
    ‘==== begin vbscript
    set fso=createobject(“scripting.filesystemobject”)
    set junk=fso.opentextfile(“data.txt”,1)
    set report=fso.createtextfile(“report.txt”)
    do while not junk.atendofstream
    line=junk.readline
    words=split(line,” “)
    for i=0 to ubound(words)
    if left(words(i),3)=”098″ or left(words(i),3)=”123″ then
    report.writeline line
    exit for
    next
    loop
    ‘===== end vbscript
    or:
    do while not junk.atendofstream
    line=” “+junk.readline
    p=instr(line,” 098″)
    if p=0 then p=instr(line,” 123″)
    if p>0 then report.writeline line
    loop
    • 0