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:
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
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.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.