Closed Finding a file name to use and passing it to a d'port

TimSmithTimSmith Member Posts: 36
There is an application that will generate CSV files for me with names in the following format:

FileYYYYMMDDHHMMSS.CSV

These files will be placed into a directory.

What I would like to do is monitor the directory, identify a file and read the earliest version, then subsequent versions if more than one example, process the data and then pass the file into a saved folder.

Does anyone have an example of this sort of process working that I could crib or add to?

Thanks
tim

Comments

  • nunomaianunomaia Member Posts: 1,153
    You have to use the virtual table file to read all the files frol that folder. Then filter those files "File*.CSV". Sort the files by name
    Nuno Maia

    Freelance Dynamics AX
    Blog : http://axnmaia.wordpress.com/
  • David_CoxDavid_Cox Member Posts: 509
    nunomaia wrote:
    You have to use the virtual table file to read all the files frol that folder. Then filter those files "File*.CSV". Sort the files by name

    If you are sure no other files will be in this folder, then no need to filter for CSV.

    You need to consider if a file fails to Import, move it to a new location, this will cover any non csv files as well.

    Use validation within the dataport, if it fails we will move the file.

    Put code like this into a report, don't use REPEAT UNTIL, just call the report via, Job Scheduler, Import one file at a time.

    Make a report with integer dataItem Number CONST(1)

    Code something like this

    FromPath := 'C:\MyFiles\';
    ToPath := 'C:\MyFiles\History\';
    ErrorPath := 'C:\MyFiles\Errors\';

    // This is a Variable Type = Record and Name = File
    ImportFiles.SETRANGE(Path,FromPath);
    ImportFiles.SETRANGE("Is a file",TRUE);
    IF ImportFiles.FIND('-')THEN BEGIN

    FromFileName := FromPath + ImportFiles.Name;
    ToFileName := ToPath + ImportFiles.Name;
    ErrorFileName := ErrorPath + ImportFiles.Name;

    IF FILE.EXISTS(FromFileName)THEN BEGIN
    // This is a Variable Type = Dataport and Name = YourDataport
    CLEAR(MyDataport);
    MyDataPort.IMPORT := TRUE;
    MyDataport.FILENAME := FromFileName;

    // Commit after the Import is required for Runmodal
    IF MyDataport.RUNMODAL THEN BEGIN
    COMMIT;
    FILE.RENAME(FromFileName,ToFileName);
    END ELSE
    FILE.RENAME(FromFileName,ErrorFileName);
    // If runmodal failed we move the file to error location
    // to allow other files to be Imported
    END;

    :)
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • TimSmithTimSmith Member Posts: 36
    :D A BIG Thanks for the info, that really helps and I now understand how the coding works.
    =D>

    Tim
  • jamesjames Member Posts: 56
    nunomaia wrote:
    You have to use the virtual table file to read all the files frol that folder. Then filter those files "File*.CSV". Sort the files by name

    Is there any code that you can use in this process that allows you to define how you want to sort the files before reading it. I need to sort by the Date Modified field otherwise the whole routine will fall over but can't work out how to get Navision to define how the folder is sorted
  • nunomaianunomaia Member Posts: 1,153
    Directly you can’t do that.
    Create a Temp table. Copy data from file virtual table to temp table. Then sort temp table the way you want.
    Nuno Maia

    Freelance Dynamics AX
    Blog : http://axnmaia.wordpress.com/
Sign In or Register to comment.