How to copy File contents using InStreams and File Operations in NAV/D365 Business Central?

Introduction:

Working with streams is fun and also tricky if we don’t get it right. I’ll be going through an example of how to read a file and copy some part of a file to another file using Instream and file operations.

The InStream (input stream) and OutStream (output stream) data types are generic stream objects that you can use to read from or write to files and BLOBs

Steps:

  1. Firstly I will explain the file and the contents I would like to copy to another File.
  • The First line describes the header of the file
  • The second line describes the product attributes
  • The DELIVERY section describes the schedule of the product for daily, weekly and monthly

2. My aim is to copy only the daily schedule of the file to another file using the below approach

  • Firstly, I’ll have to create a file and read the contents using Instream
  • Use string functions and file operation to copy the contents of the file for daily and skip the weekly and monthly schedule.

3. Declare the variables a below

4. The below code opens a file using the file path and creates instream

Clear(Filename)

Filename:=’SourceFile.txt’

Fileread.OPEN(Partner.”Source Details”+’’+Filename);
Fileread.CREATEINSTREAM(Instream_1);

Here, I have stored the File path in the a table Partner on field Source Details.

  • TEXTMODE Sets whether a file should be opened as an ASCII file or a binary file. true if the file is open for reading and writing; otherwise, false, if the file is open only for reading.
  • WRITEMODE Use this function before you use OPEN Function (File) to set or test whether you can write to a file in later calls.
  • READTEXT reads the specified number of bytes, the maximum length of the string, or until the end of the line.
  • COPYSTR Copies a substring of any length from a specific position in a string (text or code) to a new string
  • DELCHR Deletes one or more characters in a string.
  • SELECTSTR Retrieves a substring from a comma-separated string.

5. Use a While loop to read the file and do the following operations

  • Read the max length of current line using READTEXT
  • Copy the file contents of the current line using COPYSTR
  • Search for ‘ ‘ and delete them using DELCHR
  • If Current line is INTERCHG or PRODUCT write directly to file
  • If Current line is DELIVERY then check if the 6th word in a comma separated string is D then write to file else skip it.
  • Close the File writing – You can find your file in your destination path.
  • Close the File reading
  • Copy the source file to archived
  • Erase the source file.

Thanks for reading! Have a lovely weekend.

Original Post https://chrisdsilvablog.wordpress.com/2022/07/15/how-to-copy-file-contents-using-instreams-and-file-operations-in-nav-d365-business-central/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...