how to set StartPos (like in dataport) in CodeUnit?

bangswitbangswit Member Posts: 265
hi all
do you know how to set StartPos (like in dataport) in CodeUnit?
i want to export file to txt files
and there is a startpos like for example I have 3 fields
Doc No (Code 10) --> exported in txt file on position 1
Doc Name (Text 10) --> exported in txt file on position 20
Doc Date (Date) --> exported in txt file on position 30
«1

Comments

  • mrsamrsa Member Posts: 35
    you do it like:

    You open file, read whole row into one string and then parse this text into variables according to your needs:



    File.OPEN(FileName);
    File.TEXTMODE(TRUE);
    WHILE File.READ(StringFromFile) <> 0 DO BEGIN
    filedA:=COPYSTR(StringFromFile,1,10);
    IF EAVLUATE(YEARint,COPYSTR(StringFromFile,11,4)) THEN
    ...
    fieldDATE:=DMY2DATE(YEARint,MONTHint,DAYint);

    ....
    END;
  • bangswitbangswit Member Posts: 265
    thanks
    but i want to know the position to be written... not from position from the field
    for example
    doc no = 123
    doc name = test
    doc date = 010110
    so after i exported it... in txt file it should be like this
    123 test 010110

    assume 123 is in the position 1
    assume test is in the position 20
    assume date is in the position 30
  • mrsamrsa Member Posts: 35
    Same priciple:


    File.CREATE(FileName);
    File.TEXTMODE(TRUE);

    //repeat for rows you need

    StringToFILE:=Filed1+Field2+Filed3
    File.WRITE(StringToFILE);

    //end repeat

    File.CLOSE;

    You just must be aware that you wirte your self some function to adjust fields lenght. If you nedd 20 charcters lenght for one filed you must chek lenght and add empty string or zeroes from left or right side according to your need.
  • bangswitbangswit Member Posts: 265
    hmmmmmm i still don't get it
    by the way do you know how to create new line?
  • mrsamrsa Member Posts: 35
    edited 2010-07-28
    OK,
    for your exmaple:

    doc no = 123
    doc name = test
    doc date = 010110
    so after i exported it... in txt file it should be like this
    123 test 010110

    assume 123 is in the position 1
    assume test is in the position 20
    assume date is in the position 30

    Global variables:

    name type lenght
    fFile File
    Filed1 Text 100
    Filed2 Text 100
    Filed3 Text 100
    Dummy Text 50
    Dummy2 Text 10
    Dummy3 Text 10
    StringToFILE Text 38


    Code:

    Dummy:=' '; //(40 times space)
    fFile.CREATE('C:\test.txt');
    fFile.TEXTMODE(TRUE);

    //repeat for rows you need

    Field1:='123'
    IF STRLEN(Filld1)<20 THEN
    Filed1:=Field1 + COPYSTR(Dummy,1,20-STRLEN(Filed1))
    ELSE
    Filed1:=COPYSTR(Field1 ,1,20);

    Field2:='test'
    IF STRLEN(Filld2)<10 THEN
    Filed2:=Field2 + COPYSTR(Dummy,1,10-STRLEN(Filed2))
    ELSE
    Filed2:=COPYSTR(Field2 ,1,10);


    Dummy2:=FORMAT(DATE2DMY(TODAY,1));
    IF STRLEN(Dummy2)=1 THEN Dummy2:='0'+Dummy2; //Day

    Dummy3:=FORMAT(DATE2DMY(TODAY,2));
    IF STRLEN(Dummy3)=1 THEN Dummy3:='0'+Dummy3; //month

    Field3:=Dummy2+Dummy3+FORMAT(DATE2DMY(TODAY,3)); //Day+month+Year


    StringToFILE:=Filed1+Field2+Filed3
    File.WRITE(StringToFILE);

    //end repeat

    File.CLOSE;


    This should format file on to your needs
  • kitikkitik Member Posts: 230
    I once created this function, maybe you can use it:
    FillSpaces(Txt : Text[1024];Pos : 'Front,Back';MaxLen: Integer;FillChar : Text[1]) : Text[1024]
    Txt := COPYSTR(Txt,1,MaxLen);
    IF STRLEN(Txt) <> MaxLen THEN
    REPEAT
      CASE Pos OF
        Pos::Front:
          Txt := FillChar + Txt;
        Pos::Back:
          Txt := Txt + FillChar;
      END;
    UNTIL STRLEN(Txt) = MaxLen;
    
    EXIT(Txt);
    

    Examples:
    FillSpaces('9',0,10,'0')
    Return value: '0000000009'
    
    FillSpaces('My name',1,20,' ')
    Return value: 'My name             '
    
    FillSpaces('SI001',0,20,' ')
    Return value: '               SI001'
    


    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • bangswitbangswit Member Posts: 265
    kitik wrote:
    I once created this function, maybe you can use it:
    FillSpaces(Txt : Text[1024];Pos : 'Front,Back';MaxLen: Integer;FillChar : Text[1]) : Text[1024]
    Txt := COPYSTR(Txt,1,MaxLen);
    IF STRLEN(Txt) <> MaxLen THEN
    REPEAT
      CASE Pos OF
        Pos::Front:
          Txt := FillChar + Txt;
        Pos::Back:
          Txt := Txt + FillChar;
      END;
    UNTIL STRLEN(Txt) = MaxLen;
    
    EXIT(Txt);
    

    Examples:
    FillSpaces('9',0,10,'0')
    Return value: '0000000009'
    
    FillSpaces('My name',1,20,' ')
    Return value: 'My name             '
    
    FillSpaces('SI001',0,20,' ')
    Return value: '               SI001'
    


    Salut!
    Laura Nicolàs
    how about if i want use this function from 2 or 3 fields?
  • bangswitbangswit Member Posts: 265
    mrsa wrote:
    OK,
    for your exmaple:

    doc no = 123
    doc name = test
    doc date = 010110
    so after i exported it... in txt file it should be like this
    123 test 010110

    assume 123 is in the position 1
    assume test is in the position 20
    assume date is in the position 30

    Global variables:

    name type lenght
    fFile File
    Filed1 Text 100
    Filed2 Text 100
    Filed3 Text 100
    Dummy Text 50
    Dummy2 Text 10
    Dummy3 Text 10
    StringToFILE Text 38


    Code:

    Dummy:=' '; //(40 times space)
    fFile.CREATE('C:\test.txt');
    fFile.TEXTMODE(TRUE);

    //repeat for rows you need

    Field1:='123'
    IF STRLEN(Filld1)<20 THEN
    Filed1:=Field1 + COPYSTR(Dummy,1,20-STRLEN(Filed1))
    ELSE
    Filed1:=COPYSTR(Field1 ,1,20);

    Field2:='test'
    IF STRLEN(Filld2)<10 THEN
    Filed2:=Field2 + COPYSTR(Dummy,1,10-STRLEN(Filed2))
    ELSE
    Filed2:=COPYSTR(Field2 ,1,10);


    Dummy2:=FORMAT(DATE2DMY(TODAY,1));
    IF STRLEN(Dummy2)=1 THEN Dummy2:='0'+Dummy2; //Day

    Dummy3:=FORMAT(DATE2DMY(TODAY,2));
    IF STRLEN(Dummy3)=1 THEN Dummy3:='0'+Dummy3; //month

    Field3:=Dummy2+Dummy3+FORMAT(DATE2DMY(TODAY,3)); //Day+month+Year


    StringToFILE:=Filed1+Field2+Filed3
    File.WRITE(StringToFILE);

    //end repeat

    File.CLOSE;


    This should format file on to your needs
    thanks a lot for the code
    it means a lot :)
  • kitikkitik Member Posts: 230
    You can call the function as many times as needed.

    Let's say you need to write a line in this format:
    Code - 20 chars
    Name - 20 chars
    Number - 10 chars

    You could do it this way:
    Fil is a var of FILE type
    Line is a var of Text type
    
    Fil.WRITEMODE(TRUE);
    Fil.TEXTMODE(TRUE);
    Fil.CREATE(FilePathAndName);
    
    Line := '';
    Line += FillSpaces(Table.Code,0,20,' ');
    Line += FillSpaces(Table.Name,1,20,' ');
    Line += FillSpaces(Table.Number,0,10,'0');
    Fil.WRITE(Line);
    
    Fil.CLOSE;
    

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • bangswitbangswit Member Posts: 265
    kitik wrote:
    You can call the function as many times as needed.

    Let's say you need to write a line in this format:
    Code - 20 chars
    Name - 20 chars
    Number - 10 chars

    You could do it this way:
    Fil is a var of FILE type
    Line is a var of Text type
    
    Fil.WRITEMODE(TRUE);
    Fil.TEXTMODE(TRUE);
    Fil.CREATE(FilePathAndName);
    
    Line := '';
    Line += FillSpaces(Table.Code,0,20,' ');
    Line += FillSpaces(Table.Name,1,20,' ');
    Line += FillSpaces(Table.Number,0,10,'0');
    Fil.WRITE(Line);
    
    Fil.CLOSE;
    

    Salut!
    Laura Nicolàs

    sorry.... i mean i want to add position

    like this
    OutputFile.WRITE(
    FillSpaces(Table."1-System Header Tag",1,3,'0',1)+
    FillSpaces(Table."1-Partner ID",1,6,'0',20)
    );

    field Table."1-System Header Tag" --> starting position at 1
    field Table."1-Partner ID" --> starting position at 20
  • kitikkitik Member Posts: 230
    If you want to use this system, then you do not need the position
    Line := '';
    Line += FillSpaces(Table.Code,0,20,' ');
    Line += FillSpaces(Table.Name,1,20,' ');
    Line += FillSpaces(Table.Number,0,10,'0');
    

    When you create the full line you have to concatenate the fields in order.
    If field 1 has the proper lenght then field 2 is in the right position.

    If you don't like it this way, then look for SEEK function in you FILE var.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • bangswitbangswit Member Posts: 265
    kitik wrote:
    If you want to use this system, then you do not need the position
    Line := '';
    Line += FillSpaces(Table.Code,0,20,' ');
    Line += FillSpaces(Table.Name,1,20,' ');
    Line += FillSpaces(Table.Number,0,10,'0');
    

    When you create the full line you have to concatenate the fields in order.
    If field 1 has the proper lenght then field 2 is in the right position.

    If you don't like it this way, then look for SEEK function in you FILE var.

    Salut!
    Laura Nicolàs
    if there is 2 field , yes i could use front and back
    but this is using more than 5 fields
    so i must use position
    but i am confused about where and how to use SEEK
    I add 1 parameter --> FillSpaces(table."1-System Header Tag",1,19,'0',1);

    Txt := COPYSTR(Txt,1,MaxLen);
    IF STRLEN(Txt) <> MaxLen THEN
    REPEAT
    ExFile.SEEK(Position);
    CASE Pos OF
    Pos::Front:
    Txt := Fillchar + Txt;
    Pos::Back:
    Txt := Txt + Fillchar;
    END;
    UNTIL STRLEN(Txt) = MaxLen;
    EXIT(Txt);

    but it failed :(
  • mrsamrsa Member Posts: 35
    It must be like this:

    OutputFile.WRITE(
    FillSpaces(Table."1-System Header Tag",1,19,' ')+ //first filed alligned left filled by blank space must be lenght of 19 if you want to 2nd field start at 20th positiion
    FillSpaces(Table."1-Partner ID",1,6,'0')
    );

    field Table."1-System Header Tag" --> starting position at 1
    field Table."1-Partner ID" --> starting position at 20

    It' s a litle bit confusing because if you wan that some field start at position etc. 100, lenght of all fields together before must be lenght of 99
  • bangswitbangswit Member Posts: 265
    mrsa wrote:
    It must be like this:

    OutputFile.WRITE(
    FillSpaces(Table."1-System Header Tag",1,19,' ')+ //first filed alligned left filled by blank space must be lenght of 19 if you want to 2nd field start at 20th positiion
    FillSpaces(Table."1-Partner ID",1,6,'0')
    );

    field Table."1-System Header Tag" --> starting position at 1
    field Table."1-Partner ID" --> starting position at 20

    It' s a litle bit confusing because if you wan that some field start at position etc. 100, lenght of all fields together before must be lenght of 99

    yes it is.... now i use it that way
    but i think it must be easier
    if I use position
  • mrsamrsa Member Posts: 35
    with seek you must also be carefoul in wich order you use it.

    Rgards!
  • kitikkitik Member Posts: 230
    If you use position you'll have trouble filling 0s for numbers or trying to right-align a field.
    I think it is better to write the fields in order and with its proper lenght.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • bangswitbangswit Member Posts: 265
    kitik wrote:
    If you use position you'll have trouble filling 0s for numbers or trying to right-align a field.
    I think it is better to write the fields in order and with its proper lenght.

    Salut!
    Laura Nicolàs
    is it possible that we do not need using 0s
    but using position only
    can it?
  • bangswitbangswit Member Posts: 265
    and can we use this code to import the same file?
  • kitikkitik Member Posts: 230
    Try to search File.SEEK in mibuso for help.

    For reading the file you'll need some other function, it's not the same than writing!

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • kitikkitik Member Posts: 230
    bangswit wrote:
    yes it is.... now i use it that way
    but i think it must be easier
    if I use position

    I don't agree.
    I think your code will look much more clear if the line is build using the proper field order.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • bangswitbangswit Member Posts: 265
    by the way... could it be imported way using this code?
    what should I change?
  • kash387kash387 Member Posts: 111
    Hello,

    Have you tried the suggestions or solutions that you have been given by those two experts???

    if yes, then are you facing any problem while doing so???

    and if not tried, then try it....

    you will get solution from your mind about what to do next...!!!
    Thanks,

    Kashyap
  • bangswitbangswit Member Posts: 265
    kash387 wrote:
    Hello,

    Have you tried the suggestions or solutions that you have been given by those two experts???

    if yes, then are you facing any problem while doing so???

    and if not tried, then try it....

    you will get solution from your mind about what to do next...!!!
    you're not helping...

    btw many thanks for mrsa and kitik
  • kash387kash387 Member Posts: 111
    Hello Sir,

    One thing please keep in your mind, everyone who is reading your post is helping you..!!!
    By d way, sorry if you dont like... will never help you now onwards... You can continue your search...!!
    Thanks,

    Kashyap
  • bangswitbangswit Member Posts: 265
    kash387 wrote:
    Hello Sir,

    One thing please keep in your mind, everyone who is reading your post is helping you..!!!
    By d way, sorry if you dont like... will never help you now onwards... You can continue your search...!!

    everyone who is reading your post is helping you..!!! --> except you
    You're welcome....
  • kitikkitik Member Posts: 230
    kash387 wrote:
    Hello,

    Have you tried the suggestions or solutions that you have been given by those two experts???

    if yes, then are you facing any problem while doing so???

    and if not tried, then try it....

    you will get solution from your mind about what to do next...!!!

    kash387 did help you.
    Help does not mean "do my job and give me a working code".
    Help means to point you in the right direction. And kash387 did point you in the right direction by suggestion you to try something with the ideas given. Probarly then you'll figure out what to do and how to solve your specific problem.

    Salut!
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • SavatageSavatage Member Posts: 7,142
    You want to export 3 fields to a text file at set positions..so why not use a dataport?
    Add you 3 field to the Dataport Fields.
    Enter the StartPos Values 1, 20 ,30 (Even tho I think name should be longer than 10 but anyway)
    View the dataport properties & Change Fileformat to FIXED
    Change FieldStartDelimiter, FieldEndDelimeter & FileSeparator to <None>

    When you run the dataport you can select export & enter a file name C:\File.txt
    Or those can set via code automatically.

    The dataport will also allow to import.

    Any reason your stuck using a codeunit?
  • bangswitbangswit Member Posts: 265
    Savatage wrote:
    You want to export 3 fields to a text file at set positions..so why not use a dataport?
    Add you 3 field to the Dataport Fields.
    Enter the StartPos Values 1, 20 ,30 (Even tho I think name should be longer than 10 but anyway)
    View the dataport properties & Change Fileformat to FIXED
    Change FieldStartDelimiter, FieldEndDelimeter & FileSeparator to <None>

    When you run the dataport you can select export & enter a file name C:\File.txt
    Or those can set via code automatically.

    The dataport will also allow to import.

    Any reason your stuck using a codeunit?
    yes I know
    using dataport is more easily...
    but how about import for line number 2 (record no 1)
    there is no startpos for line number 2, am I right?
  • SavatageSavatage Member Posts: 7,142
    bangswit wrote:
    yes I know
    using dataport is more easily...
    but how about import for line number 2 (record no 1)
    there is no startpos for line number 2, am I right?

    It's hard to answer that becasue We don't know what table you using and if it has line no's as a field?
  • bangswitbangswit Member Posts: 265
    Savatage wrote:
    bangswit wrote:
    yes I know
    using dataport is more easily...
    but how about import for line number 2 (record no 1)
    there is no startpos for line number 2, am I right?

    It's hard to answer that becasue We don't know what table you using and if it has line no's as a field?

    no .. i mean data like this
    SHR ED 93AEAN007ORDERS0 --> Line 1
    HDRORDERS E1010 603516 2010041620100417
    603516 8888383388838 01 Cold Storage 32 QUALITY ROAD
    --> Line 2
    line 1 is no problem because ony 1 line
    line 2 there are 2 lines... how to set start post for line 2 --> starting from 603516
    that is my question
    thanks
Sign In or Register to comment.