how to create a codeunit for a text file

ChristChrist Member Posts: 24
Hello, i created a dataport to import a text file. Problem is that this process has to be automated through NAS. Nas doesn't support dataport so i have to create a codeunit to import the text file. This is rather new for me. Can anyone give me some info/code how to read in a text file through codeunit. Kind regards

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    I would go for a report or a XMLport, but you could also use codeunit. Maybe DataType File fits for you, but it depends on the structure of the file you want to import.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • ChristChrist Member Posts: 24
    what is the advantage of using a report instead of a codeunit? I'm new in this stuff so i would choose the most easy way to go with. It's just a file with a komma separator per record line.
  • krikikriki Member, Moderator Posts: 9,110
    If 1 line is less than 1024 chars, you can read line per line and analyse it to separate the different fields.

    A report has no advantages. Best use a codeunit.
    Best use tab separated. It is safer than ","-separated.

    The code is something like this:
    txtTab := 'x'; // txtTab is text of len 1
    txtTab[1] := 9;
    
    fil.TEXTMODE(true);
    fil.WRITEMODE(false);
    IF NOT fil.OPEN('c:\subdir\file.txt') then
      error('Could not open');
    intLen := fil.LEN;
    
    WHILE intlen > fil.POS do begin // do not use always fin.LEN. It slows down a lot
      clear(txtString);
      fil.READ(txtString);
    
      // analyse the txtString
      blnStop := FALSE;
      REPEAT
        intTabPos := STRPOS(txtString,txtTab);
        IF intTabPos <> 0 THEN BEGIN
          txtValue := COPYSTR(txtString,1,intTabPos - 1);
          txtString := COPYSTR(txtString,intTabPos + 1);
        END
        ELSE BEGIN
          txtValue := txtString;
          blnStop := TRUE;
        END;
        // do something with your field
        ...
      UNTIL blnStop;
    END;
    fil.CLOSE;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • SogSog Member Posts: 1,023
    kriki wrote:
    If 1 line is less than 1024 chars, you can read line per line and analyse it to separate the different fields.

    A report has no advantages. Best use a codeunit.
    I find the ; the best seperator (together with the [] fieldstart/end delimiter)

    But a report has an advantage, it can be run from the "start-search" (or run depending on version) to run a link like [url=navision://.....]navision://.....[/url], a codeunit does not have that option. (for the rest indeed no advantages)
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • krikikriki Member, Moderator Posts: 9,110
    Sog wrote:
    kriki wrote:
    If 1 line is less than 1024 chars, you can read line per line and analyse it to separate the different fields.

    A report has no advantages. Best use a codeunit.
    I find the ; the best seperator (together with the [] fieldstart/end delimiter)

    But a report has an advantage, it can be run from the "start-search" (or run depending on version) to run a link like [url=navision://.....]navision://.....[/url], a codeunit does not have that option. (for the rest indeed no advantages)
    The possibility that in a description there is a ; or a , is too high. I have had that a lot of times. The TAB is less likely (only happens if users copy info from Excel).
    An even better separator is "MAHABHARATA" in the hope no one stores info from Hindu religion in NAV. :mrgreen:
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ChristChrist Member Posts: 24
    I tried it out and everything works fine but for me the problem is how to cut to the next line. So the first line works fine but the secon doesn't. The text file is like this: 1;1;1;1:1
    2;2;2;2;2
    so the field delimiter is ; and record delimiter is newline but how to solve the new line so that is is been seen as a new record?
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    Set TEXTMODE of your file variable to TRUE then READ will return one line to you.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
Sign In or Register to comment.