REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Count lines in a file quickly





 
/* sample code to count the lines in a file quickly                   */
/*                                                                    */
/* Source: I found this code in a message on the internet             */
/*                                                                    */
/* see also Count lines in a file quickly - 2 -                       */
/*                                                                    */

                        /* name of the file                           */
  testFile = "C:\OS2\INISYS.RC"

  call time "r"

  noOfLines = CountLines( testFile )

  if noOfLines >= 0 then
  do
    say "The file " || testFile || " contains " || ,
        noOfLines || " lines."
    say "It took " || time( "e" ) || " seconds to get the number."

  end /* if */
  else
    say "Error reading the file " || testFile

exit 0

/* ------------------------------------------------------------------ */
/* function: Get the number of lines in a file                        */
/*                                                                    */
/* call:     CountLines( fileName )                                   */
/*                                                                    */
/* where:    fileName - name of the file                              */
/*                                                                    */
/* returns:  n                                                        */
/*             if n is >= 0 then it is the number of lines            */
/*             if n is < 0 an error occured                           */
/*                                                                    */
CountLines: PROCEDURE
  parse arg fileName

  if stream( fileName, "c", "QUERY EXISTS" ) <> "" then
  do
    if stream( fileName, "c", "OPEN READ" )  = "READY:" then
    do

      thisRC = 0
      do while chars( fileName ) > 0

        thisRC = thisRC + ,
         words( translate( charin( fileName, , 24020 ), "          1" ) )

      end /* do while chars( fileName ) > 0 */
      call stream fileName, "c", "close"

    end /* if stream( fileName, "c", "open read" )  = "READY:" then */
    else
      thisRC = -1

  end /* if stream( fileName, "c", "QUERY EXISTS" ) <> "" then */
  else
    thisRC = -2

RETURN thisRC

  

Inf-HTML End Run - Successful