Appending to the end of a file - BC v14, C/AL

mark02tj
Member Posts: 6
Hello - I am trying to append lines to an existing file in BC v14 (although this could pertain to NAV 2018 as well). I am opening/creating a file as shown below:
TempFileName := FileMgt.ServerTempFileName('txt');
OutputFile.WRITEMODE := TRUE;
OutputFile.TEXTMODE := TRUE;
IF EXISTS(TempFileName) THEN
OutputFile.OPEN(TempFileName)
ELSE OutputFile.CREATE(TempFileName);
OutputFile.CREATEOUTSTREAM(OutputFileStream);
Encoding := Encoding.ASCII;
StreamWriter := StreamWriter.StreamWriter(OutputFileStream, Encoding);
Note: this seems to be pretty common code and if I remember correctly, I found this on Microsoft's site several years ago.
This code works well and to populate the file, I use "Streamwriter.WriteLine(FileString)". Eventually I close the file after the last line is written.
Currently I have a project where I want to open a file that I've previously created and append more lines to the end of the file. Is there an easy way to do this in C/AL?
When searching online for an answer, I've seen some posts that indicate that you might be able to do this by first reading the existing file, re-outputting the existing contents back to the file and then writing more lines to the file. There are two issues with this approach - First, I'm not sure how to do it.
Next, the file has the potential to become many thousands of lines long and I'm not sure how long it would take to read and rewrite a 30,000 line file. Hence the reason for me asking if there's an "easy way" to accomplish this. Also, this is kind of a one-time thing. Once this large file is created, we won't be creating it again so I'm trying to minimize the development time, while also trying to avoid creating several hundred smaller files and copy/pasting them all into a larger file.
THANKS!!
TempFileName := FileMgt.ServerTempFileName('txt');
OutputFile.WRITEMODE := TRUE;
OutputFile.TEXTMODE := TRUE;
IF EXISTS(TempFileName) THEN
OutputFile.OPEN(TempFileName)
ELSE OutputFile.CREATE(TempFileName);
OutputFile.CREATEOUTSTREAM(OutputFileStream);
Encoding := Encoding.ASCII;
StreamWriter := StreamWriter.StreamWriter(OutputFileStream, Encoding);
Note: this seems to be pretty common code and if I remember correctly, I found this on Microsoft's site several years ago.
This code works well and to populate the file, I use "Streamwriter.WriteLine(FileString)". Eventually I close the file after the last line is written.
Currently I have a project where I want to open a file that I've previously created and append more lines to the end of the file. Is there an easy way to do this in C/AL?
When searching online for an answer, I've seen some posts that indicate that you might be able to do this by first reading the existing file, re-outputting the existing contents back to the file and then writing more lines to the file. There are two issues with this approach - First, I'm not sure how to do it.

THANKS!!
0
Answers
-
You cannot do this with C/AL streams, as there is no way to position them. Since you have a file (not a Blob) you can use FILE:
// upload your existing file to the server and get that file's full filename OutputFile.WRITEMODE := TRUE; OutputFile.TEXTMODE := TRUE; OutputFile.OPEN(TempFileName, TEXTENCODING::MSDos); OutputFile.SEEK(OutputFile.LEN); OutputFile.WRITE(FileString); OutputFile.CLOSE; // download the file again
I don't know if you could use .NET streams. Check out codeunits 3034 DotNet_Stream, and 3025 DotNet_StreamWriter.1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions