How to read csv file from the server and data in ax 2012

Description:-

In this article we will see about how to read File from Folder and csv file data from the server path using X++.

static void ReadCsvfile_Data(Args _args)
{
    #File
    System.IO.DirectoryInfo     directory;
    System.IO.FileInfo[]        files;
    System.IO.FileInfo          file;
    InteropPermission           permission;
    Filename                    tmpFilePath;
    Filename                    tmpFileNameShort;
    Filename                    tmpFileExt;
    CommaTextIo                 importFile;
    IO                          iO;
    str                         fileNameTemp;
    FilenameOpen                filename;
    counter                     filesCount;
    counter                     loop;
    str                         filePath,fileNameOnly;
    filetype                    type;
    container                   record;
    str                         Delimiter = ",";

    permission  = new InteropPermission(InteropKind::ClrInterop);
    permission.assert();

    directory   = new System.IO.DirectoryInfo(@"D:\");
    files       = directory.GetFiles("*.csv*");
    filesCount  = files.get_Length();

    for (loop = 0; loop < filesCount; loop++)
    {
        file                                        = files.GetValue(loop);
        fileNameTemp                                = file.get_FullName();
        [tmpFilePath, tmpFileNameShort, tmpFileExt] = fileNameSplit(fileNameTemp);
        filename = fileNameTemp;
        info(strFmt("filename: %1",filename));
        iO = new CommaTextIo(filename,#IO_Read);
        if (! iO || iO.status() != IO_Status::Ok)
        {
            throw error("@SYS19358");
        }
        record = iO.read();
        while(iO.status() == IO_Status::Ok)
        {
            record = iO.read();
            if(!record)
            break;
            info(strFmt("Csv file data %1",conpeek(record, 1),conpeek(record, 2)));
        }
    }
    CodeAccessPermission::revertAssert();
}

Related Posts

Previous
Next Post »

Thanks for comments.....