Page 1 of 2 12>
Topic Options
#13205 - 2001-10-15 06:19 PM Count lines in a file
Anonymous
Unregistered


Could you advise on what I need to do to count the number of lines in a given file, and then write oout the result, along with the filename to another file.

Thanks.

Top
#13206 - 2001-10-15 07:57 PM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Try something like this:
code:

Break on
$count = 1
$FileName = "C:\boot.ini"
IF Open(1, $FileName) = 0
$x = ReadLine(1)
WHILE @ERROR = 0
? "Line number " + $count + " reads: [" + $x + "]"
$count = $count + 1
$x = ReadLine(1)
LOOP
ENDIF

get $


_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13207 - 2001-10-15 08:07 PM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Oops! In my haste to post, (trying tobeat Shawn ) missed second requirement.
code:

Break on
$count = 0
$FileName = "C:\boot.ini"
$ResultFile = "C:\Result.txt"
IF Open(1, $FileName) = 0
$x = ReadLine(1)
WHILE @ERROR = 0
$count = $count + 1
? "Line number " + $count + " reads: [" + $x + "]"
$x = ReadLine(1)
LOOP
ENDIF
Close(1)

IF Open(2, $ResultFile,5) = 0

$x = WriteLine( 2 , "File " + $FileName + " has " + $count + " lines" + Chr(13) + Chr(10) )
ELSE
BEEP
? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

get $


_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13208 - 2001-10-17 04:38 AM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Is this what you had in mind? Let me know how you made out.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13209 - 2002-11-28 01:45 AM Re: Count lines in a file
LilmanHSU Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 6
Loc: Calabasas, CA
LLigetfa,

That code worked like a charm!

I modified it a bit.. but I wanted to ask you. We want to use the counting feature that you wrote to basically count lines of dir.txt and if there are more than ten lines, to run a script that would output the text into an email.

Also, if there less than 10, but more than zero to run a different script that would output the text into an email?

Would that be nested if then statements?

Ultimately what we want to do, and maybe others can offer some suggestions, is that if the result is greater than zero, but less than 10 (this by the way represents number of sites that did not backup across the WAN if you are curious) to email an account here and we'll take care of it in the morning.

If its greater than 10, then we need design a paging script that will take the output file from above (10+ sites) attach the plain text to email, send it, and then wait for a response (how it will do that I don't know, but my boss does it with SCO unix box)

If it doesn't get a response from the first person on the list, it tries the next etc.. until someone shoots back a message that stops the paging sequence and takes care of the problem..

Here is my modifed version:

Break on
$count = 0
$FileName = "C:\branches\dir.txt"
$ResultFile = "C:\branches\Result.txt"
IF Open(1, $FileName) = 0
$x = ReadLine(1)
WHILE @ERROR = 0
$count = $count + 1
$x = ReadLine(1)
LOOP
ENDIF
IF Open(2, $ResultFile,5) = 0
$x = WriteLine( 2 , "Log " + $FileName + " reports " +

$count + " down sites" + Chr(13) + Chr(10) )
ELSE
BEEP
? "failed to open file, error code : [" + @ERROR + "]"
ENDIF
;get $
QUIT

Anyone have any suggestions.. I know this is rather complex.. Even a software package that exists will suffice..

Top
#13210 - 2002-11-28 01:51 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
could you post your code in codetags please.
_________________________
!

download KiXnet

Top
#13211 - 2002-11-28 02:09 AM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well, the IF stuff is simple.
code:
if $count > 0 and $count < 10
'do something' ?
endif

The email stuff is pretty straight forward as well. Search the board for the word "blat" and you'll get lots of examples. There are other ways besides blat but you will likely get some in your search.

The escalation though will take a bit of thought. How do you plan to stop the escalation?

I have in place on my network two notification systems that use escalation. Both are commercial 3rd part apps. One is Whatsup Gold from www.ipswitch.com and the other, our paging system that uses software from http://www.emergin.com als has ways for recipients to stop the escalation.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13212 - 2002-11-28 02:35 AM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well, maybe something like this is better.
code:
if $count > 0
if $count < 10
'do something' ?
else
'do something else' ?
endif
endif

Forgot about the second req if 10 or greater.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13213 - 2002-11-28 03:40 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
maybe even:
code:
if $count
if $count < 10
'do something' ?
else
'do something else' ?
endif
endif

[Razz]
_________________________
!

download KiXnet

Top
#13214 - 2002-11-28 03:43 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, tell me what is wrong in my code?
_________________________
!

download KiXnet

Top
#13215 - 2002-11-28 03:45 AM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
same as mine... less 2 strokes [Big Grin]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13216 - 2002-11-28 03:50 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yep [Wink]

and if it were a golf, it should be written as:
code:
if $count
if $count < 10
;do something here
else
;and something else here
endif
endif

just wonder, sreg has been running for 40 minutes and still processor usage 99%... [Eek!] should I interrupt it? [Roll Eyes]
_________________________
!

download KiXnet

Top
#13217 - 2002-11-28 04:46 AM Re: Count lines in a file
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Question

Would this work if the actual count of lines was not important?

code:
  
Break on
$count = 0
$FileName = "C:temp.txt"
IF Open(1, $FileName) = 0 and ReadLine(1)
WHILE @ERROR = 0
$count = $count + 1
if $count = 10
call "count at least 10 script"
return/exit
endif
? "Line number " + $count + " reads: [" + $x + "]"
$x = ReadLine(1)
LOOP
call "less than ten but greater than 0 script"
ENDIF
Close(1)
get some$

_________________________
Eric

Top
#13218 - 2002-11-28 05:00 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
maciep, do you mean that this should do then too:
code:
$FileName = "C:temp.txt"
$=Open(1, $FileName)
do
$count = 1+$count
until 0=ReadLine(1) or $count >10
if @error=0
if $count>10
call "count at least 10 script"
else
call "less than ten but greater than 0 script"
endif
endif
$=Close(1)
get $

[Wink]
_________________________
!

download KiXnet

Top
#13219 - 2002-11-28 05:21 AM Re: Count lines in a file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, so the IF part is easy enough. How would you do the email escalation thing?

Guess that would depend on the email system and how to COM inbound messages. Figure you'd need to have a rule to catch a reply to stop the escalation. I run a Notes shop so haven't seen anything dealing with inbound. I do outbound with SMTP.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#13220 - 2002-11-28 05:28 AM Re: Count lines in a file
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Nice!

So when is that next golf tournament?
_________________________
Eric

Top
#13221 - 2002-11-28 08:03 AM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I quess it's the golf-king who decides on that...
been promising for some 6 weeks but. we will see.

it just happens that jens is not here on weekends so how could he be hosting such an event...
_________________________
!

download KiXnet

Top
#13222 - 2002-11-28 05:27 PM Re: Count lines in a file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
I know I'm late to this dance... [Wink]
For reading #n lines of a file, have a look at UDFs ReadFileHead and ReadFileTail
_________________________
We all live in a Yellow Subroutine...

Top
#13223 - 2002-11-28 05:32 PM Re: Count lines in a file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
waltz, late and you lost in golf score too [Wink]
_________________________
!

download KiXnet

Top
#13224 - 2002-11-28 05:46 PM Re: Count lines in a file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
The story of my life?
And I'm more into ice hockey than golf so I code extra stuff to get the score up rather than down... [Big Grin]
_________________________
We all live in a Yellow Subroutine...

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.254 seconds in which 0.194 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org