Monday, November 25, 2013

Convert Tab file to CSV and Load


Just a tip for the day.

If you got a tab limited file and want to read it as a CSV file, using the file layout, use the below code snippet.

Local File &TABFILE, &CSVFILE;
Local string &FILE_NAME, &DATA, &NEWDATA;

&FILE_NAME = "Test.txt";
&TAB = Char(9);

&TABFILE = GetFile(&FILE_NAME, "r");
&FileName = &TABFILE.Name;
&POS = Find(".", &FileName);
&NEWFILE_NAME = Substring(&FileName, 1, &POS) | "dat";
&CSVFILE = GetFile(&NEWFILE_NAME, "N", %FilePath_Absolute);
If &TABFILE.IsOpen And
      &CSVFILE.IsOpen Then
   While &TABFILE.ReadLine(&DATA);
      &NEWDATA = Substitute(&DATA, &TAB, ",");
      &CSVFILE.WriteLine(&NEWDATA);
   End-While;
   &TABFILE.Close();
   &CSVFILE.Close();
End-If;

Will be handy...

3 comments:

  1. Or you can change your File Layout to use Tab Delimiter instead of comma.

    ReplyDelete
  2. Daniel,

    Seems like that option is not there anymore.

    Varun

    ReplyDelete
  3. Tab delimiter is available at least as of PT 8.53.08

    ReplyDelete

Total Pageviews

Copyright © Tech & Management Notes