Inoke C# code to find number of lines in text file having millions of records?

I want to use the below code with invoke code activity to find the number of lines in a text file.

Code:

public static long CountLinesMaybe(Stream stream)  
{
    Ensure.NotNull(stream, nameof(stream));

    var lineCount = 0L;

    var byteBuffer = new byte[1024 * 1024];
    const int BytesAtTheTime = 4;
    var detectedEOL = NULL;
    var currentChar = NULL;

    int bytesRead;
    while ((bytesRead = stream.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
    {
        var i = 0;
        for (; i <= bytesRead - BytesAtTheTime; i += BytesAtTheTime)
        {
            currentChar = (char)byteBuffer[i];

            if (detectedEOL != NULL)
            {
                if (currentChar == detectedEOL) { lineCount++; }

                currentChar = (char)byteBuffer[i + 1];
                if (currentChar == detectedEOL) { lineCount++; }

                currentChar = (char)byteBuffer[i + 2];
                if (currentChar == detectedEOL) { lineCount++; }

                currentChar = (char)byteBuffer[i + 3];
                if (currentChar == detectedEOL) { lineCount++; }
            }
            else
            {
                if (currentChar == LF || currentChar == CR)
                {
                    detectedEOL = currentChar;
                    lineCount++;
                }
                i -= BytesAtTheTime - 1;
            }
        }

        for (; i < bytesRead; i++)
        {
            currentChar = (char)byteBuffer[i];

            if (detectedEOL != NULL)
            {
                if (currentChar == detectedEOL) { lineCount++; }
            }
            else
            {
                if (currentChar == LF || currentChar == CR)
                {
                    detectedEOL = currentChar;
                    lineCount++;
                }
            }
        }
    }

    if (currentChar != LF && currentChar != CR && currentChar != NULL)
    {
        lineCount++;
    }
    return lineCount;
}

Studio:

Errors:

image

we can use just an assign activity

filePath: String variable with the full FilePath to the file
C#:
fileCount = File.ReadAllLines(filePath).Length;

VB:
fileCount = File.ReadAllLines(filePath).Length

1 Like

Thanks for your reply.

Yes I have tried this code, it is working fine for the files having less number of records. But when the text file is having records in millions, Uipath executor is stopped working giving below exception.

Therefore, I was trying to use the invoke code to invoke this custom code to read lines from a large text file.

Thanks

give a try at:

fileCount = File.ReadLines(filePath).Count();

as other options, we can shift to e.g. buffered readers, stream readers, etc…

1 Like

Hi,

The following is a sample using StremReader, FYI.

count = 0;
using (var sr = new StreamReader(filePath))
{
    while (sr.ReadLine() != null) {count++; }
}

Sample20220420-1.zip (2.6 KB)

Regards,

1 Like

Tried this:

fileCount = File.ReadLines(filePath).Count;

it was successful. Took 4 minutes to count file of 20 million records.

Thanks

Tried this:

count = 0;
using (var sr = new StreamReader(filePath))
{
    while (sr.ReadLine() != null) {count++; }
}

it was successful and fast. Took 20 seconds to count file of 20 million records.

Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.