Read a File Using File Layout

Your rating: None Average: 4.8 (19 votes)

A sample code to read a file using file layouts and insert data read into a record.

Local File &MYFILE;
Local Record &REC;
Local array of string &ARRAY;
Local string &FILE_DIRECTORY, &FileName;

&FileName = "MY_FILE_NAME.txt";
&FILE_DIRTORY = "/MYDIRECTORY/"
/*open file for reading*/
&MYFILE = GetFile(&FILE_DIRECTORY | &FileName, "R", %FilePath_Absolute);
/*create record object*/
&REC = CreateRecord(Record.MY_RECORD);
&ARRAY = CreateArrayRept("", 0);
/*check if file is open*/
If &MYFILE.IsOpen Then
/*The SetFileLayout method is a file layout method. It associates a specific file layout definition with the file object executing this method, providing easy access to rowset data.(PeopleBooks)*/
   If &MYFILE.SetFileLayout(FileLayout.FILE_LAYOUT_NAME) Then
/*read line into &STRING*/
      While &MYFILE.ReadLine(&STRING);
         &ARRAY = Split(&STRING, ",");
         For &I = 1 To &REC.FieldCount
            &REC.GetField(&I).Value = RTrim(LTrim(&ARRAY [&I]));
         End-For;
         /* do additional processing here for converting values */
         &REC.Insert();
/*count rows inserted into record*/
         &COUNT = &COUNT + 1;
      End-While;
   Else
      /* do error processing - filelayout not correct */
   End-If;
Else
   /* do error processing - file not open */
End-If;

I hope this was short and to the point!

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.
Guest's picture
Guest (not verified)
Re: Read a File Using File Layout

Thank you, nice article

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
The question below is to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.