Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Creatingand reading the text file

(0) ShareShare
ReportReport
Posted on by

Hello All,

i have one requirement where i have to create a new file for logging errors and i have written the code like below:

In the below code it is writing only one line in text file.

Example i have 4 line that needs to be written in ErrorLog file but it is writing only last line.

One more case is that if that errorlog text already exists then it should read that file and at last line it should start inserting the records.

I am not getting how to find the last line of the text file and copy these records

Please suggest.


ErrorFileName:='D:\Custom Clearance\ErrorLog.txt';
//ErrorFileName := 'ErrorLog.txt';
datDateTime:=CURRENTDATETIME;
IF recItem.GET(Item1) THEN BEGIN
IF NOT CustClrInfo.GET(ActivityType,DateofClr,Itemno,variant1) THEN BEGIN
CustClrInfo.INIT;
CustClrInfo."Activity Type" := ActivityType;
CustClrInfo."Date of Customs Clearance":=DateofClr;
CustClrInfo."Item No.":=Item1;
CustClrInfo."Variant Code":=variant1;
CustClrInfo.Quantity:=Qty;
CustClrInfo.INSERT(TRUE);
END;
END ELSE BEGIN


{IF EXISTS(ErrorFileName) THEN BEGIN
ErrorFile.TEXTMODE(TRUE);
ErrorFile.WRITEMODE(FALSE);
ErrorFile.OPEN(ErrorFileName);
REPEAT
ErrorFile.READ(ErrorFileName);
UNTIL ErrorFile.POS=ErrorFile.LEN;                                                          //not getting how to find the last line and then insert these errors
FileOutStream.WRITETEXT(Dummy);
FileOutStream.WRITETEXT();
ErrorFile.CLOSE;
//FileOutStream.WRITETEXT(FORMAT(datDateTime));
MESSAGE('1');
END ELSE BEGIN
ErrorFile.CREATE(ErrorFileName);
ErrorFile.CREATEOUTSTREAM(FileOutStream);
FileOutStream.WRITETEXT(Dummy);
FileOutStream.WRITETEXT();
ErrorFile.CLOSE;
//FileOutStream.WRITETEXT(FORMAT(datDateTime));
MESSAGE('Hi');

END;
}END;

*This post is locked for comments

  • Suggested answer
    Binesh Profile Picture
    7,885 on at
    RE: Creatingand reading the text file

    Hello,

    As per other members suggestion go thru,

    Otherwise you can see this screenshot and based on your requirement change this [Not Tested].

    So first use in test DB.

    CreateFIle.png

  • Suggested answer
    Suresh Kulla Profile Picture
    45,180 on at
    RE: Creatingand reading the text file

    As Rajasekhar said you just need to set the position for the file using ErrorFile.POS(ErrorFile.LEN) you don't need the repeat statement. For your other issue of getting only one i see only one WRITETEXT statement and it is not in the loop, so it is creating only one line.

  • Community Member Profile Picture
    on at
    RE: Creatingand reading the text file

    sorry not understood your code lines and tried with the same as in my code but its not working.

    THANKS

  • Suggested answer
    Rajasekhar@MS Profile Picture
    5,567 on at
    RE: Creatingand reading the text file

    Hi

    After opening the file Set the position to the end like below.

    ErrorFile.TEXTMODE(TRUE);

    ErrorFile.WRITEMODE(FALSE);

    ErrorFile.OPEN(ErrorFileName);

    ErrorFile.POS(ErrorFile.LEN);

    and then you can continue writing to the file.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans