Options

Pass FILENAME to Dataport - Virtual Table Update ?

allenmanallenman Member Posts: 37
Hello all.

Some of what I am asking help on has been covered in other posts, indeed I have recieved help with this problem in the relevent area.
However, as more than one technique may be possible, instead of posting to multiple threads I have decided to start a new one with a full list.
I hope this is acceptable.

I am still new to programming Navision, this is only my second project. I have not started this new thread lightly ( I have used the searches) and I have spent the last week on and off looking at this problem. As I am still stuck I want to give it one last try. Apologies if I have broken any rules with this.

I wish to import multiple "|" delineated text files using a dataport from a fixed directory. As these text files are processed they are moved to an achive dir. New text files may be added at any time.

My problem to resolve is how to pass the text file name to the dataport.

Method 1
This involves using the virtual table "File" 2000000022.
It has been duscussed here http://www.mibuso.com/forum/viewtopic.php?t=9635&highlight=dataport and here http://www.mibuso.com/forum/viewtopic.php?t=1633&highlight=virtual
My code is called from the form to loop calls to the dataport dependent on record variable file.name.
FilePath2 :='C:\RGJ\';

CLEAR(MyFile2);
MyFile2.RESET;
MyFile2.SETRANGE(Path,FilePath2);
MyFile2.SETRANGE("Is a file",TRUE);
MyFile2.SETFILTER(Name, '@*.txt');
IF MyFile2.FIND('-') THEN BEGIN
  REPEAT
    DATAPORT.RUN(50038, FALSE)
  UNTIL MyFile2.NEXT = 0;
END ELSE BEGIN
  MESSAGE ('All text files have been processed.');
  EXIT;
END;

The dataport code looks like this:
Dataport - OnPreDataport()
FilePath :='C:\ABC\';

//MyFile.INIT;
CLEAR(MyFile);
MyFile.RESET;
MyFile.SETRANGE(Path,FilePath);
MyFile.SETRANGE("Is a file",TRUE);
MyFile.SETFILTER(Name, '@*.txt');
IF MyFile.FIND('-') THEN BEGIN
  REPEAT
    FileLength :=STRLEN(MyFile.Name)- 4;
    PSSName := COPYSTR(MyFile.Name,1,FileLength);
          IF NOT SdcRecord.GET(PSSName) THEN BEGIN
               FileName :=MyFile.Name;
               CurrDataport.FILENAME(FilePath + FileName);
               EXIT;
           END;
 // IF MyFile.NEXT = 0 THEN EXIT;
  UNTIL MyFile.NEXT = 0;
END;
  CurrDataport.QUIT;
  MESSAGE ('All text files have been processed.');
  


Dataport - OnPostDataport()
CompletedPath :='C:\ABC\Completed\';
CurrFile.CLOSE;

COPY(FilePath + FileName, CompletedPath + FileName);

WHILE EXISTS(CurrDataport.FILENAME)
DO ERASE(CurrDataport.FILENAME);

This almost works. During a first pass the files data are correctly inserted and the files coped to the new directory and then the original deleted.
I have two problems with this:

1- If I rerun the code I find that the virtual table has not been updated, it still sees the original file list. So my code does not run the dataport if the primary key alrady exists. But I still get a Navision error of
The operating system cannot find the drive and directory specified for the file .
Please check that the drive, directory and file name are correct.
I just cannot get my head around this error. Can anyone help me stop this message ?

2- As new files are added to the directory because the virtual table is not updated they are not detected. If I shut the form down and re-open it is sometimes ok. Can anyone help me "re-fresh" the virtual table?

Method 2
How To pass parameters between objects (forms, reports, ...)?
http://www.mibuso.com/howtoinfo.asp?FileID=7&Type=howto

I found this really interesting and informative.
I do not use a User Request Form, so I tried loading the text file name into global variables but the info was lost between the calling of the function from my form to the calling of the dataport from the same form.
I was thinking along the lines of:
IF MyFile2.FIND('-') THEN BEGIN
REPEAT
CLEAR(MyDataPort);
FileName := MyFile2.Name;
MyDataPort.fctSetFileName(FileName,FilePath);
DATAPORT.RUNMODAL(50038, FALSE)
UNTIL MyFile2.NEXT = 0;
END ELSE BEGIN
MESSAGE ('All text files have been processed.');
EXIT;
END;
END;
and
PROCEDURE fctSetFileName@1000000000(pFileName@1000000000 : Text[40];pFilePath@1000000001 : Text[50]);
    BEGIN
      FileName := pFileName;
      FilePath := pFilePath;
      //DATAPORT.RUNMODAL(50038, FALSE);
    END;

So instead, could I use a uesr request form to hold the passed text filename but have it hidden and processed automatically?


Method 3
I have also come across this technique which avoids the use of the virtual table.
[SOLVED] Windows Script Host Object Model Folders and Files.
http://www.mibuso.com/forum/viewtopic.php?t=7855&highlight=virtual

I have not been able to make any in-roads into using this. If anyone agrees this may help could they provide some more info on how to implement it?


I know I am asking a lot I can only apologise.

Regards,
Steve

Answers

  • Options
    kinekine Member Posts: 12,562
    For importing you can use something like that:
    CLEAR(Files);
    Files.RESET;
    Files.SETFILTER(Path,'C:\');   //to refresh data if the GetImportDir is still same 
    Files.SETFILTER(Path,'%1',GetImportDir);
    Files.SETFILTER(Name,'%1','*.txt');
    Files.SETRANGE("Is a file",TRUE);
    IF Files.FIND('-') THEN
      REPEAT
        CLEAR(Imp);
        Imp.FILENAME(GetImportDir+Files.Name);
        Imp.RUNMODAL;
        COMMIT;
      UNTIL Files.NEXT=0;
    

    The Imp is variable of type DATAPORT.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kinekine Member Posts: 12,562
    Dataport - OnPostDataport()
    CompletedPath :='C:\ABC\Completed\';
    CurrFile.CLOSE;
    
    COPY(FilePath + FileName, CompletedPath + FileName);
    
    WHILE EXISTS(CurrDataport.FILENAME)
    DO ERASE(CurrDataport.FILENAME);
    

    May be that problem is that CurrDataport.FILENAME is cleared when the CurrFile.CLOSE is called...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    Tarek_DemiatiTarek_Demiati Member Posts: 112
    Regarding the Error
    The operating system cannot find the drive and directory specified for the file.
    Please check that the drive, directory and file name are correct.

    As you've figured out the File record set has not been
    updated, the trick is to make Navision point to another
    path and then set it back to path you would like to scan.
    MyFile.RESET;
    MyFile.SETRANGE("Is a file",TRUE); 
    // >> [Fix Refresh Problem] - Tarek 06.04.2006
    MyFile.SETRANGE(Patch, 'C:\'); // Set it to any existing path which is different than FilePath
    IF MyFile.FIND('-') THEN; // Force Navision to retrieve the first file
    // << [Fix Refresh Problem] - Tarek 06.04.2006
    MyFile.SETRANGE(Path,FilePath); 
    MyFile.SETFILTER(Name, '@*.txt'); 
    

    Kamil :
    If you do not perform a IF MyFile.FIND('-') THEN; after the SETRANGE Navision do not update the record set (at least on my system WinXP with Nav 3.6)
  • Options
    kinekine Member Posts: 12,562
    Yes, it is possible, that is part from old object of 2.60 database... :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    allenmanallenman Member Posts: 37
    kine and Tarek Demiati, thank you so much.
    To get it fully working I needed to repeat the Find as Tarek Demiati stated.
    Thank you both for the content and speed of your replies.

    For anyone else here is a summery of the working code
    FilePath2 :='C:\ABC\';
    Root := COPYSTR(FilePath2,1,3);
    CompletedPath :='C:\ABC\Completed\';
    
    CLEAR(MyFile2);
    MyFile2.RESET;
    
    MyFile2.SETRANGE("Is a file",TRUE);
    MyFile2.SETFILTER(Path,Root); //Refresh Virtual Table if data changed
    IF MyFile2.FIND('-') THEN; // Force Navision to retrieve the first file
    
    
    MyFile2.SETFILTER(Path,'%1',FilePath2);
    MyFile2.SETFILTER(Name,'%1', '@*.txt');
    MyFile2.SETRANGE("Is a file",TRUE);
    IF MyFile2.FIND('-') THEN BEGIN
      REPEAT
        CLEAR(MyDataPort);
        MyDataPort.FILENAME(FilePath2 + MyFile2.Name);
        MyDataPort.RUNMODAL;// DATAPORT.RUN(50038, FALSE)
    
        FILE.COPY(FilePath2+MyFile2.Name, CompletedPath+MyFile2.Name);
        WHILE EXISTS(FilePath2+MyFile2.Name)
        DO ERASE(FilePath2+MyFile2.Name);
        
        COMMIT;
      UNTIL MyFile2.NEXT = 0;
    END ELSE BEGIN
      MESSAGE ('All text files have been processed.');
      EXIT;
    END;
    

    Variables:
    Name	     DataType   Subtype
    MyFile2	     Record	      File	
    MyDataPort    Dataport    CourierDP
    
Sign In or Register to comment.