Solved Vbscript to search a text file for a particular string

December 21, 2012 at 02:04:55
Specs: Windows 7

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...

See More: Vbscript to search a text file for a particular string

Report •

December 21, 2012 at 14:19:23
✔ Best Answer
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
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
'===== end vbscript
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

Report •

December 21, 2012 at 15:12:08
tks sir the code is working but it writes only that number and not that entire line in which the string is available..

I want the entire line written to the text file.. thanx in advance

Report •

December 21, 2012 at 15:18:10
You seem to use "line" and "word" interchangeably, which confused me. You want the entire line, not just the word, right? I will go with that. I will edit my first response, so look for the changes there.

Report •
Related Solutions

Ask Question