Importing files with NAS

CarloM
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)
Hope this helps some people.
Succes.
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
0
Comments
-
You can also use XMLports if you use v5 or v2009.
Regards0 -
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 is0 -
dans wrote:why not use codeunit to call the dataport ?
Dataports do not work with at all with an Navision Application Server (NAS)0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions