Options

Line of file

AsallaiAsallai Member Posts: 141
Hi,

Can you explain me how can I get the line number of a text file? :-k
Any solution. :roll:

Thank you! :)

Comments

  • Options
    ArhontisArhontis Member Posts: 667
    Hello,

    If you perform a sequential read to the file, i.e. from a dataport, you can add an int var and on the OnAfterGetRecord increase it.

    You can use the same concept anytime you read or write to a file...

    It is either simple, or I didn't got your question.
  • Options
    AsallaiAsallai Member Posts: 141
    I'd like to open a file from a codeunit. But first I have to check the number of the lines in the file.
    I created a Var of File. This case the LEN parameter is the size of the file and not the number of rows. This not good for me.
    I'd like to check the number of rows in simple.

    Summary: how many rows in a text file? :?:
  • Options
    ArhontisArhontis Member Posts: 667
    Hi,

    If it is essential to have the number of lines, then you could build a function like:
    PROCEDURE GetLineCount@1000000005(aFilename@1000000000 : Text[1024]) : Integer;
        VAR
          aFile@1000000001 : File;
          NoOfLines@1000000002 : Integer;
          aJunkLine@1000000003 : Text[250];
          BytesRead@1000000004 : Integer;
        BEGIN
          aFile.OPEN(aFilename);
          aFile.TEXTMODE(TRUE);
          NoOfLines:=0;
          aFile.READ(aJunkLine);
          WHILE aFile.POS<=aFile.LEN DO BEGIN
            NoOfLines:=NoOfLines+1;
            BytesRead := aFile.READ(aJunkLine);
            IF (aFile.POS=aFile.LEN) AND (BytesRead = 0) THEN BEGIN
              aFile.CLOSE;
              EXIT(NoOfLines);
            END;
          END;
          aFile.CLOSE;
          EXIT(NoOfLines);
        END;
    

    and call it from your code prior reading the file.

    It was "fast made" but returns the no of lines in a file...

    It took a couple of secs to count about 50000 lines of a file.

    Can't think anything else...
  • Options
    AsallaiAsallai Member Posts: 141
    This is works at my first test method, thank you. :D
    Great! :)
  • Options
    TirtaTirta Member Posts: 53
    IF (aFile.POS=aFile.LEN) AND (BytesRead = 0) THEN BEGIN
    aFile.CLOSE;
    EXIT(NoOfLines);

    Hi...,
    i've just tried the function, and it does return the number of line but I still got an error "The file is not open". Do you have any idea? I didn't use Return Value.
  • Options
    TirtaTirta Member Posts: 53
    Ok.. I've got it. I put Exit; after command File.Close.
    Case closed.
Sign In or Register to comment.