Importing files with NAS

CarloMCarloM Member Posts: 5
Dataports are not supported when you're using the NAS.
So when you have to import CSV files of files with Fixed width then you have a problem.
I encountered the same problem and I want to share my solution.

I've created a single instance codeunit with 4 functions.
- OpenFile
- CloseFile
- ReadLine : reads a CSV with ; as separator
- ReadLineFiels : reads a file with fields where the fieldlenght is fixed (startposition and endposition of a field)
//-----------------------------------------------
//Function to open a file and create an instream
//Global variables declared
//   gFileStream	InStream		
//   gFile	             File	
//-----------------------------------------------	
OpenFile(pFilePath : Text[250]) Ok : Boolean 
Ok := TRUE;
IF gFile.OPEN(pFilePath) THEN
  gFile.CREATEINSTREAM(gFileStream)
ELSE
  Ok := FALSE;

//---------------------------------------------------------------
// Function to read ; separated values from a file
// Local Variables
// lChar	Text		1
// lArrayPosition	Integer		
//
// When you use this function you should declare an array like in the 
// parameter of the function. The valuse will be in that array.
// As you can see the number of fields and length of the fields is limited to 250
// (which can be changed)
//---------------------------------------------------------------
ReadLine(VAR pValues : ARRAY [250] OF Text[250]) Ok : Boolean
lArrayPosition := 1;
CLEAR(pValues);
Ok := TRUE;
WHILE NOT gFileStream.EOS DO BEGIN
  gFileStream.READTEXT(lChar,1);
  IF NOT (lChar = '') THEN BEGIN
    IF NOT (lChar = ';') THEN BEGIN
      pValues[lArrayPosition] := INSSTR(pValues[lArrayPosition],lChar,9999);
    END ELSE
      lArrayPosition += 1;
  END ELSE BEGIN
    EXIT;
  END;
END;
Ok := lArrayPosition > 1;

//--------------------------------------------------------------------------------------------
//Function to read fixed width values from a file
// Local variables
// lArrayPosition	Integer		
// lChar	            Text		1
// lPos	             Integer	
// 
// Before calling thsi function you should fill the array pFiledStart with the startpositions of the fields 
// and pFieldEnd with the endpositions of the fields.
// --------------------------------------------------------------------------------------------------
ReadLineFields(VAR pValues : ARRAY [250] OF Text[250];pFieldStart : ARRAY [250] OF Integer;pFieldEnd : ARRAY [250] OF Integer) Ok : Boo
lArrayPosition := 1;
lPos := 1;
CLEAR(pValues);
Ok := TRUE;
WHILE NOT gFileStream.EOS DO BEGIN
  gFileStream.READTEXT(lChar,1);
  IF NOT (lChar = '') THEN BEGIN
    IF(lPos >= pFieldStart[lArrayPosition]) THEN BEGIN
      IF lPos <= pFieldEnd[lArrayPosition] THEN BEGIN
        pValues[lArrayPosition] := INSSTR(pValues[lArrayPosition],lChar,9999)
      END ELSE BEGIN
        lArrayPosition += 1;
        pValues[lArrayPosition] := INSSTR(pValues[lArrayPosition],lChar,9999)
      END;
    END;
    lPos+= 1;
  END ELSE BEGIN
    EXIT;
  END;
END;
Ok := lArrayPosition > 1;

//--------------------------------
//Function to close the file
//--------------------------------
CloseFile()
gFile.CLOSE();

Hope this helps some people.
Succes.
CMY

Comments

  • ClausHamannClausHamann Member Posts: 80
    You can also use XMLports if you use v5 or v2009.

    Regards
  • dansdans Member Posts: 148
    why not use codeunit to call the dataport ?
    Microsoft Certified IT Professional for Microsoft Dynamics NAV

    Just a happy frood who knows where his towel is
  • SogSog Member Posts: 1,023
    dans wrote:
    why not use codeunit to call the dataport ?
    Because, as is known and specified in the documentation of NAV. wait, let me put this loud and clear.

    Dataports do not work with at all with an Navision Application Server (NAS)
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
Sign In or Register to comment.