Search code examples
openedgeprogress-4gl

How can I split a.csv file in Progress4GL if it exceeds 1000000 lines


In my program I am outputting a .csv file which exceeds 1000000 lines. Currently after the file is exported, I am splitting the file from linux using the below commands. However, I would like to know if we can split the files using a progress code. If so, could someone plese let me know on how to do it.

Below is the linux command I use manually.

ls -l xa*
split -1000000 filename.csv
mv xaa filename-01.csv
mv xab filename-02.csv

Solution

  • Without any code to work with I invented some code outputting to different files. You will have to work with OUTPUT TO and set new filenames.

    This example will output 1050 lines split in files of 100 lines each.

    DEFINE VARIABLE iLinesToOutput AS INTEGER NO-UNDO INIT 1050.
    DEFINE VARIABLE iSplitAt       AS INTEGER NO-UNDO INIT  100.
    
    DEFINE VARIABLE iLine AS INTEGER   NO-UNDO.
    DEFINE VARIABLE cFile AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iFile AS INTEGER   NO-UNDO.
    DEFINE VARIABLE iOpen AS INTEGER   NO-UNDO.
    
    DEFINE STREAM str.
    
    DO iLine = 1 TO iLinesToOutput:
        
        // Open a new stream/file    
        IF (iLine - 1 ) MOD iSplitAt = 0 THEN DO:
            iFile = iFile + 1.
            cFile  = "c:\temp\file-" + STRING(iFile, "999") + ".txt".
            OUTPUT STREAM str TO VALUE(cFile).
            EXPORT STREAM str DELIMITER "," "Customer ID" "Order Number" "Contact" "Count"
        END.
        
        // Output some data
        PUT STREAM str UNFORMATTED "Line " iLine SKIP.
        
        // Close the stream/file 
        IF iLine MOD iSplitAt = 0 THEN DO:
            OUTPUT STREAM str CLOSE.
        END.
        
    END.
    
    /* Close last file if not exactly right number of lines */
    /* This could also be checked but close twice doesn't really matter */
    OUTPUT STREAM str CLOSE.