Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of lines in a text file without opening it?

I'm developing this code that, after the user selects a directory, it display a table of the files contained in that location with their details (name, type, size ...).

A directory may contain a lot of files.

I succeeded to achieve that. But, my problem is that I want to display the number of lines in each file. I can get the number of lines using this JavaScript code :

var reader = new FileReader();
var textFile = $("#file").get(0).files[0];
reader.readAsText(textFile);
$(reader).on('load', processFile);
/*And in processFile() i use this line to get the number of lines :*/
nbLines = (file.split("\n")).length;

The above code work as expected and it give me what I want, but it may a heavy process if there is so many files in the selected directory!

The Question : Is there a way to get the number of lines in a text file without reading it?

Regards!

like image 576
Hamza Abdaoui Avatar asked Dec 04 '22 21:12

Hamza Abdaoui


1 Answers

You can't count the number of lines in a file without reading it. The operating systems your code runs on do not store the number of lines as some kind of metadata. They don't even generally distinguish between binary and text files! You just have to read the file and count the newlines.

However, you can probably do this faster than you are doing it now, if your files have a large number of lines.

This line of code is what I'm worried about:

nbLines = (file.split("\n")).length;

Calling split here creates a large number of memory allocations, one for each line in the file.

My hunch is that it would be faster to count the newlines directly in a for loop:

function lineCount( text ) {
    var nLines = 0;
    for( var i = 0, n = text.length;  i < n;  ++i ) {
        if( text[i] === '\n' ) {
            ++nLines;
        }
    }
    return nLines;
}

This counts the newline characters without any memory allocations, and most JavaScript engines should do a good job of optimizing this code.

You may also want to adjust the final count slightly depending on whether the file ends with a newline or not, according to how you want to interpret that. But don't do that inside the loop, do it afterward.

like image 181
Michael Geary Avatar answered Dec 06 '22 12:12

Michael Geary