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
0
Comments
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;
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
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.
by the way do you know how to create new line?
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
Examples:
FillSpaces('9',0,10,'0') FillSpaces('My name',1,20,' ') FillSpaces('SI001',0,20,' ')
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.
it means a lot
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:
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.
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
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
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
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
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
Rgards!
I think it is better to write the fields in order and with its proper lenght.
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.
but using position only
can it?
For reading the file you'll need some other function, it's not the same than writing!
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.
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
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
what should I change?
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...!!!
Kashyap
btw many thanks for mrsa and kitik
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...!!
Kashyap
everyone who is reading your post is helping you..!!! --> except you
You're welcome....
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!
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
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?
http://www.BiloBeauty.com
http://www.autismspeaks.org
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?
http://www.BiloBeauty.com
http://www.autismspeaks.org
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