VBSCript to read text in a line and output1/0

January 21, 2010 at 09:00:25
Specs: Windows XP
Hi to All,
I am a newbie so would really appreciate some help.
I have a text file with several lines and I want to do two things.
1. Search through a file which has a several lines of text but I am only interested in lines which contain ‘=>’ (eg shown below)
scrsep.png=>2 PNG 800×600=>200×200 800×600+300+200 8-bit DirectClass 0.000u 0:00.015
2. Once I find that line, I want to be able to read the last 3 digits (015), compare it with a value say (050) and if it is less then 50, write 0 in an output file. If it is greater then (050), then write 1 in the output text file.

Eventually I want to be able to use this string of final 1’s and 0’s stored in the output file to display a green or red cell on a web page respectively.
Would it be possible for you to give me some guidance? I really appreciate that.
Kind Regards
Annie Johnson


See More: VBSCript to read text in a line and output1/0

Report •


#1
January 21, 2010 at 22:04:31
i really suck at vbs, but here's my lame attempt, maybe you can work from it (msgboxes are for diagnosis, like "echo"):

dim fso
Path1=".\"
Set fso = createobject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder(Path1)
File="test"
Dir=Path1
Fileout="testout"
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
' .\test
Set oFile = fso.OpenTextFile (Dir&Fileout, ForAppending, True)
Set iFile = fso.OpenTextFile (Dir&file, ForReading)
do while iFile.AtEndOfStream=false
x=iFile.readline
if instr(x,"=>")>0 then
msgbox x
ll=len(x)-2
ff=mid(x,ll,3)
if ff>50 then kk=1 else kk=0
ofile.writeline kk
msgbox kk
end if
loop

Wscript.Quit


Report •

#2
January 22, 2010 at 08:22:36
Hi , I don’t know how to thank you but this is almost exactly what I was looking for!

Once again, thank you for your help.
Regards
Annie


Report •

#3
January 28, 2010 at 05:50:03
Hi,
I have one more question related to this and would be grateful for some help.
I have a text file which only has one digit in each row; this digit is either 0 or 1

I want to be able to read this text file line by line and if I encounter 0 in any row, then I want to save the result in a new text file giving the row number where 0 was found
For example, if my input text file is:
1
1
0
1

Then the output text file will say:
Found in row 3

Kind Regards
Annie


Report •

Related Solutions

#4
January 28, 2010 at 07:48:52
One last question:
If I have a text file as follows:
all: 8144.4 (0.124276)

How can I read the first two digits after the decimal point within brackets? As the number of decemial places can be anywhere between 4 and 6, I need to read 2 places after decimal (from left to write) (i.e. 12)
Please help.
Kind Regards
Annie


Report •

#5
January 28, 2010 at 12:31:03
plug this into the core of the code in resp #1 (ie: inside the do while loop):

n=n+1
if x=0 then
ofile.writeline("zero at row " & n)
end if

(initialize var. n to zero prior to the do-while loop): n=0

part 2, (var. Y contains the string you described like:
76(12.8962) read in from file as was done above)

p=instr(y,"(")
if p>0 then
p2=instr(y,p+1,".")
if p2>0 then
y=mid(y,p+1,p2-p+2)
end if
end if


Report •

#6
January 29, 2010 at 02:39:25
Hi thanks again for your reply, really helpful. One last question I have on part 1 is:

If my text file was like
1
0
1
1
0

Is there a way to write the found ‘0’s’ in a single line so that the output file says:
zero found at 3, 5

Also if no 0 is found in any row, how can I write one line to output file saying:
No zero found

Kind Regards

Annie


Report •

#7
January 29, 2010 at 12:11:23
yeah, try something like (pre-loop):
n=0
zeros=""

(inside loop)
if x=0 then zeros=zeros&","&n

(outside loop:)
if zeros="" then zeros="no zeros found" else zeros="zeros at lines: "&zeros
ofile.writeline zeros


Report •

#8
February 1, 2010 at 07:19:07
Hi , sorry I am a bit lost. Is that how you mean?
n=0
zeros=""
do while iFile.AtEndOfStream=false
x=iFile.readline
if x=0 then
zeros=zeros&","&n
n=n+1
else
end if
if zeros="" then zeros="no zeros found" else zeros="zeros at lines: "&zeros
ofile.writeline zeros
loop

Kind Regards
Annie


Report •

#9
February 1, 2010 at 12:15:11
close! just do some surgery:
n=0
zeros=""
do while iFile.AtEndOfStream=false
x=iFile.readline
n=n+1
if x=0 then zeros=zeros&","&n
loop

if zeros="" then zeros="no zeros found" else zeros="zeros at lines: "&zeros
ofile.writeline zeros


Report •

#10
February 4, 2010 at 08:58:09
Thanks a million for your patience and guidance :)

Report •


Ask Question