Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Quick way to find number of lines in 16GB file
Message
From
09/06/2020 18:35:13
 
 
To
09/06/2020 17:50:37
General information
Forum:
Windows
Category:
Computing in general
Miscellaneous
Thread ID:
01674670
Message ID:
01674804
Views:
35
Here's a version that works with files larger than 2GB.
count_lines_over_2gb("C:\big.csv")


FUNCTION count_lines_over_2gb
LPARAMETERS tcFilename
LOCAL lnHandle, lnLines, lcData, lnSize, lnLast

    * Validate parameter
    IF TYPE("tcFilename") != "C" OR NOT FILE(tcFilename)
        MESSAGEBOX("Usage:  lines filename.ext", 0 + 64, "Count Lines")
        CANCEL
    ENDIF

    DECLARE INTEGER _sopen IN msvcr71.dll STRING cFilename, INTEGER nMode, INTEGER nSharing, INTEGER nPermission
    DECLARE INTEGER _read  IN msvcr71.dll INTEGER nHandle, STRING@ outData, INTEGER outDataLength
    DECLARE INTEGER _close IN msvcr71.dll INTEGER nHandle

    * Open the file (binary, do not deny shared access)
    lnHandle = _sopen(tcFilename, 0x8000, 0x40, 0)
    IF lnHandle <= 0
        MESSAGEBOX("Error opening " + tcFilename, 0 + 64, "Count Lines")
        CANCEL
    ENDIF

    * Count the lines
    lnSize  = 0
    lnLast  = 0
    lnLines = 0
    DO WHILE .T.
        IF INT(lnSize / 10000000) != lnLast
            WAIT WINDOW TRANSFORM(INT(lnSize / 1024000)) + " MB" NOWAIT
            lnLast = INT(lnSize / 10000000)
        ENDIF
        lcData  = SPACE(16384000)
        lnLength = _read(lnHandle, @lcData, LEN(lcData))
        IF lnLength = 0
            EXIT
        ENDIF
        lnLines = lnLines + OCCURS(CHR(13), lcData)
        lnSize  = lnSize  + lnLength
    ENDDO
    _close(lnHandle)

    * Report
    MESSAGEBOX("File contains " + LTRIM(TRANSFORM(lnLines, "999,999,999")) + " lines." + CHR(13) + "File is " + LTRIM(TRANSFORM(lnSize, "99,999,999,999,999")) + " bytes", 0 + 64, "Count Lines")
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform