Read a File Using File Layout

Your rating: None Average: 4.5 (30 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

Guest's picture
Guest
Re: Read a File Using File Layout

Useful :D

Guest's picture
Guest
Re: Read a File Using File Layout

If we want to fetch particular field from csv file how we can do it using Array?

Like In CSV filed, columns are in one order and in PS Table fields are in one order??

Any leads will be appreciated...