Tuesday, March 17, 2015

Retrieving/Picking files from Directory through X++

Following piece of code to pick all files from a directory.


static void readFromDirectory(Args _args)
{
    #Evat_NL
    #File
   
    // variable declration to retrieve files from folder
    Filename            baseFolder;
    Filename            pdfFilename;
    Filename            foundBaseFileName;
    Filename            foundFileName;
   
    container           mainFolder, subFolder, fileContainer;
    boolean             filesFoundMainFolder = true;
    boolean             filesFoundSubFolder = true;
   
    int                 apiResult;   
    int                 startTime, endTime, fileCounter;
   
    RecordInsertList    recordList = null;

    int setCurrentFolder (Filename _filename = '')
    {
        ;
        return WinAPI::setCurrentDirectory(_filename);
    }
    ;  
   
    baseFolder = @"C:\Test\invoices\"; // Reading all pdf invoices from a directory

    apiResult           = setCurrentFolder(SysTreeNode::duplicatePathDelimiters(baseFolder));
    mainFolder          = WinAPI::findFirstFile("*.pdf*");
    foundBaseFileName   = conpeek(mainFolder, 2);   

    startTime = WinAPI::getTickCount();

    while (filesFoundMainFolder)
    {
        if (foundBaseFileName != #currentFolder && 
            foundBaseFileName != #upFolder && 
            foundBaseFileName != '')
        {
            // Concatenating each file with base folder to get full path
            // For example; C:\Test\invoices\Invoice1.pdf
            pdfFilename = baseFolder + foundBaseFileName;

            fileCounter++;
        }
        apiResult            = setCurrentFolder(SysTreeNode::duplicatePathDelimiters(baseFolder));
        foundBaseFileName    = WinAPI::findNextFile(conpeek(mainFolder, 1));
        filesFoundMainFolder = foundBaseFileName ? true : false;
    }

    endTime = WinAPI::getTickCount();

    info(strFmt('It took %1 minutes to read %2 pdf files', ((endTime - startTime)/1000)/60, fileCounter));
}

No comments:

Post a Comment

I will appreciate your comments !

Download large bacpac (sandbox database) to DEV environment much faster

As the LCS website gets slower and slower and the database backups get bigger and bigger.  Use AZCopy to download objects out of LCS asset l...