Search This Blog

Monday, 16 February 2015

X++ code to fetch the fileName from Directory based on Current Company Code

Hi ,

Refer below code which will fetch the filename from Directory (FilePath) based on Current Company Code


 static container findMatchingFiles(
        str _folderPath
    ,   str _filePattern   = '.txt')
{
    System.IO.DirectoryInfo     directory;
    System.IO.FileInfo[]        files;
    System.IO.FileInfo          file;
    InteropPermission           permission;
    str         fileName;
    counter     filesCount;
    counter     loop;
    container   mathchingFiles;
    ;
    permission  = new InteropPermission(InteropKind::ClrInterop);
    permission.assert();
     //BP Deviation Documented
    directory   = new System.IO.DirectoryInfo(_folderPath);
    files       = directory.GetFiles(_filePattern);
    filesCount  = files.get_Length();
    for (loop = 0; loop < filesCount; loop++)
    {
        file            = files.GetValue(loop);
        fileName        = file.get_FullName();
        mathchingFiles  = conins(mathchingFiles, conlen(mathchingFiles) + 1, fileName);
    }
    CodeAccessPermission::revertAssert();
    return mathchingFiles;
}

static FileName fetchFiles(Args _args)
{
    container   files;
    counter     loop;
    FileName         fileName;
  
    str entityId;
   
    Filename filepathVal;
    Filename fileType;
    filename fileNameString;
    ;
    entityId=strLfix('E'+curext(),7,'0');
    files = DNT_DatabasicsUpload::findMatchingFiles(#FilePath, entityId+'*.txt');
    for (loop = 1; loop <= conlen(files); loop++)
    {
   
        filename= conpeek(files, loop);
        return filename;
      
    }
    return #FilePath;
}

Happy Daxing...............

Friday, 13 February 2015

X++ code to import data from text file(.txt) to Ax Tables in Ax 2009

Hi ,

Refer the below X++ code which import data from text file to Ax Tables in AX 2009
 
static void Notepad(Args _args)
{
FilenameOpen filename;
dialogField dialogFilename;
Dialog dialog;
TextIO file;
NotepadTable notepadTable;
container con;
str conitem,conitem1,conitem2;
int i;
#File
 
;

dialog = new Dialog("Notepad Upoad");
dialogFilename = dialog.addFieldValue(extendedTypeStr(FilenameOpen),filename,"File Name");
dialog.filenameLookupFilter([".txt", #AllFiles]);
dialog.filenameLookupTitle("Upload from Text File");
dialog.caption("Upload from text file");
dialogFilename.value(filename);
if(!dialog.run())
return;
filename = dialogFilename.value();
file = new TextIO(filename, #IO_READ);
file.inRecordDelimiter('\n'); //For Next Record
file.inFieldDelimiter('\t');
ttsbegin;
while(file.status() == IO_STATUS::OK)
{
con = file.read();
conitem = conpeek(con,1);
if(conItem!="0")
{
notepadTable.initValue();
notepadTable.Name=conpeek(con,1);
notepadTable.MathsMark=conpeek(con,2);
notepadTable.PhysicalMark=conpeek(con,3);
notepadTable.insert();
}
 
 
 
}
ttscommit;