Split big text files in smaller ones in X++


Below is a job I found useful many times, so don’t want to loose it.
Nothing special, but maybe you’ll find it useful too.
It splits an input text file into smaller files.
As there is one comment line for every line of code (almost), I’m sure you’ll figure it out :-).
static void KlForSplitFile(Args _args)
{
    #File
    AsciiIO             inFile, outFile;
    container           rec;
    int                 cnt = 0, fileNum    = 0;
    FileName            outFileName;
    FileIOPermission    filePermissionOrig, outFilePermission;
    Set                 permissionSet = new set(types::Class);

    // settings begin
    // input file
    str                 _inFileName = @"C:\temp\testsplit\testsplit.csv";
    // max number of lines
    int                 _maxrec = 20000;
    //settings end
    ;

    //
    // first loop to create the permission set
    //

    // assert permission for read
    new FileIOPermission(_inFileName, #io_read).assert();

    // create new asciiIo for the file we are reading in
    inFile  = new AsciiIo(_inFileName,#io_read);

    // read file to check how many files will be created
    while(inFile.read())
    {
        if(cnt == _maxrec || cnt == 0)
        {
            // 1 more file
            filenum++;
            // create filename
            outFileName = strfmt('%1_%2.csv',
            substr(_inFileName,1,strlen(_inFileName) - 4), fileNum);
            // create permission
            outFilePermission = new FileIOPermission(outFileName,#io_write);
            // add permission to set
            permissionset.add(outFilePermission);
            // reset line counter
            cnt = 0;
        }
        // 1 more line
        cnt++;
    }

    // also add fileIn permission to set
    filePermissionOrig =  new FileIOPermission(_inFileName, #io_read);
    permissionset.add(filePermissionOrig);
    // revert permission assertion
    CodeAccessPermission::revertAssert();

    // assert permissions for set
    CodeAccessPermission::assertMultiple(permissionset);

    //
    // loop file again and split
    //

    // reset counters
    fileNum = 0;
    cnt     = 0;

    // create new asciiIo for the file we are reading in
    inFile  = new AsciiIo(_inFileName,#io_read);

    // read file to check how many files will be created
    // read ahead
    rec = inFile.read();
    while(rec)
    {
        if(cnt == _maxrec || cnt == 0)
        {
            // 1 more file
            filenum++;
            // create filename
            outFileName = strfmt('%1_%2.csv',
            substr(_inFileName,1,strlen(_inFileName) - 4), fileNum);
            outFile = new AsciiIO(outFileName,#io_write);
            // reset line counter
            cnt = 0;
        }
        // 1 more line
        cnt++;
        // write to split file
        outFile.writeExp(rec);
        // read next line
        rec = inFile.read();
    }

    // revert permission assertion
    CodeAccessPermission::revertAssert();

    info('done');
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.