Importing a txt file with a dataport

Hello all, Another small problem, i need to build a dataport that imports data from a text file, however it should not import the first and last line of the text, is there any way to accomplish this? Thank you very much in advance, Kind Regards, Robin

Various easy ways: 1st way) Open the text file with a text editor and remove the first and the last lines. 2nd way) set autosave property to no, set an integer variable to 0 on predataitem.on AfterImportRecord copy the value of the imported record to other variable of the same type and increase the counter (the integer variable). If the counter >1 then modify the copy of the record (it will be the previous record read). That way you’ll skip the 1st record (1st line) and then just write all except the last line. another way for skiping the first lines is adding an empty dataitem (with no dataport fields) and making it read just the first line, breaking after it. Regards Alfonso Pertierra (Spain)apertierra@teleline.es

Alfonso, I understand the easy way (1st) and, for one time use, think it’s good. But I don’t understand how to know you’re on the last record in the 2nd way? You have to have some kind of lookahead to say “I’m on the last record, don’t write this one out.” How do you do that? Dave Studebaker das@libertyforever.com Liberty Grove Software A Navision Services Partner

David, To skip the last entry you can delete the last inserted value on PostDataItem. There is another way of doing it which I like. Put the following code on OnAfterImportRecord IF CurrFile.POS = CurrFile.LEN THEN CurrDataport.SKIP; Satbir

Simple and stupid: 1) Insert the record normally. 2) OnPostDataItem delete the record. With best regards from Switzerland Marcus Fabian

I knew you smart guys would set me straight. Thanks. Dave Studebaker das@libertyforever.com Liberty Grove Software A Navision Services Partner

quote:


David wrote: Alfonso, I understand the easy way (1st) and, for one time use, think it’s good. But I don’t understand how to know you’re on the last record in the 2nd way? You have to have some kind of lookahead to say “I’m on the last record, don’t write this one out.” How do you do that.


No… you don’t need to keep any lookahead or something similar is just using the standard way navision works: skip the 1st record. (counter = 1) if (the counter is higher than 2) then begin if (not insert auxiliarvariablerecord) then modify (auxiliarvariablerecord) end; auxiliarvariablerecord := ImportedRecord; That way, on the 3rd loop you’ll be inserting the 2nd record, on the 4th loop, the 3rd record… when ending at the n loop, you’ll be inserting the n-1 record and the last record (n) won’t be inserted. The method of deleting the record after importing it it’s not good if you’ve previous data in the application, as sometimes you’ll find that the last record is an existant one and you want to keep it and not to change it’s values… if deleting it you’ll be loosing data you don’t want to. Dataitemtype1 myDataItemRecord var counter integer; myauxrecord record mydataportdataitemtype; set autosave property to No. OnPredataitem counter := 0; OnAfterImportRecord counter += 1; IF (counter =1) THEN BEGIN CurrDataport.SKIP; //We skip the first record. END ELSE BEGIN IF (counter >2) THEN IF (NOT myauxrecord.INSERT) THEN myauxrecord.MODIFY; MyAuxRecord := myDataItemRecord; END; Regards, Alfonso Pertierra (Spain)apertierra@teleline.es

Robin, Navision provides standard file functions which can be used. You can always write extra variables and code associated with them to achieve the solutions. ------------- To skip first record write the following code on OnBeforeImportRecord IF CurrFile.POS = 0 THEN CurrDataport.SKIP; To skip Last record write the following code on OnAfterImportRecord IF CurrFile.POS = CurrFile.LEN THEN CurrDataport.SKIP; ------------------------ Regards, Satbir

One last question about this last solution, i want to use the following code to skip the first record: IF CurrFile.POS = 0 THEN CurrDataport.SKIP; When I use this in the OnBeforeImportRecord of the dataport and the first line in the csv has some columns with a different type then specified in the dataport, it will not work, because it still does a check wheter the types are identical. Thanks very much again, Kind Regards, Robin van den Boom