skipping dataport-- need help

GilGil Member Posts: 49
Here i have is a code that will automatically load textfiles on dataport, but is there any way to skip the current textfile if it encounters an error?
Folder := 'C:\Sales Import';
FileSystem.SETRANGE(FileSystem.Path, Folder);
FileSystem.SETRANGE(FileSystem."Is a file", TRUE);
FileSystem.SETFILTER(FileSystem.Name, '@*.txt');
IF FileSystem.FIND('-') THEN BEGIN
  REPEAT
    //MESSAGE(FileSystem.Name);

    old_name := '';
    new_name := '';  

    file_name := FileSystem.Name; //for counting

    old_name := FileSystem.Path + '\' + FileSystem.Name;
    new_name := FileSystem.Path + '\' + file_name;

    //MESSAGE('%1',STRLEN(file_name));
    file_to_import := FileSystem.Path + '\' + FileSystem.Name;
    IF STRLEN(file_name) = 19 THEN BEGIN
      //MESSAGE(FORMAT(STRLEN(file_name)));
    IF EXISTS(file_to_import) THEN BEGIN
       //MESSAGE(FileSystem.Name);
       IF FileSystem.Size <> 0 THEN BEGIN
           //CLEAR(DataportObject);
           //DataportObject.SetFileName(file_to_import);
           DataportObject.FILENAME(file_to_import);
           DataportObject.IMPORT(TRUE);
           DataportObject.RUNMODAL;

         [b] ///SKIP HERE ON ERROR <--------------[/b]
        END;

        //to rename in to new name
        RENAME(old_name, new_name);
        IF COPY(FileSystem.Path + '\' + FileSystem.Name, FileSystem.Path + '\Imported\' + FileSystem.Name) THEN
           //MESSAGE(old_name);
           ERASE(FileSystem.Path  + '\' +FileSystem.Name);

        
    END;
    END;



  UNTIL FileSystem.NEXT = 0;
END;
MESSAGE('DONE');

Comments

  • FranNavFranNav Member Posts: 5
    I think the route you need at the end '\'

    Folder := 'C:\Sales Import\'

    EDIT: sorry, I just realized that this is not the error
  • GilGil Member Posts: 49
    as you can see i set the dataport.filename = textfile then runs it..
    i dont know how to skip the textfile if it encounters any error
  • FranNavFranNav Member Posts: 5
    What is the error that can be found? if you know, I can help
  • FDickschatFDickschat Member Posts: 380
    Do no run the dataport from your function but encapsulate it inside a Codeunit and run the CU with if CU.run instead:

    Instead of
    Gil wrote:
    IF FileSystem.Size <> 0 THEN BEGIN
      //CLEAR(DataportObject);
      //DataportObject.SetFileName(file_to_import);
      DataportObject.FILENAME(file_to_import);
      DataportObject.IMPORT(TRUE);
      DataportObject.RUNMODAL;
    
      [b] ///SKIP HERE ON ERROR <--------------[/b]
    END;
    

    Do something like this:
    IF FileSystem.Size <> 0 THEN BEGIN
      clear(CUFileImport);
      CUFileImport.SetParamas(...)
      if CUFileImport.run... then
        ...
    END;
    
    Inside of CUFileImport:
      //CLEAR(DataportObject);
      //DataportObject.SetFileName(file_to_import);
      DataportObject.FILENAME(file_to_import);
      DataportObject.IMPORT(TRUE);
      DataportObject.RUNMODAL;
    
    Frank Dickschat
    FD Consulting
  • GilGil Member Posts: 49
    i don't think this is a solution.
    i'd give you a scenario, if there was an error on textfile lets say:

    !1!|!Sales!|!00A0000001!|!LOCAL!|!000100!|

    But i insert instead
    ?!1!|!Sales!|!00A0000001!|!LOCAL!|!000100!|

    we would a receive an error that it cant import.

    On my code it has a loop, when getting an error like this it should skip that textfile
  • dansdans Member Posts: 148
    FDickschat already gave you the solution. put it into codeunit and use IF Codeunit.RUN THEN.
    Nav will not throw any error message, and will continue executing the next command.
    Microsoft Certified IT Professional for Microsoft Dynamics NAV

    Just a happy frood who knows where his towel is
Sign In or Register to comment.