Search code examples
powerbuildersave-asdatawindowdelimited

Powerbuilder-Remove last separator


Hi i'm using PB 12 and I have problems with SaveasFormattedText

The funcion works well, but the separator char add one character at the end of each line and I need to delete the last separator.

For example, saveasformattedtext export data like this:

data1;data2;data3;data4; 
data1;data2;data3;data4; 
etc. 

And I need like this:

data1;data2;data3;data4 
data1;data2;data3;data4 

without the ";" at the end.

this my code :

dw_report.SaveAsFormattedText("d:/RESULTS2.TXT", EncodingUTF8! , "|", "","~r~n",True)

Please help


Solution

  • Try the below function. You might want to add some error checking and more options as you require. the idea is to save each row separately as a line in the file.

    /*function: f_dw_saveasformattedtext(dw_1, ls_filename, ';')
    parameters: adw_dw(datawindow/datastore), as_filename (string), as_separator(string)
    */
    long ll_row, ll_rows, ll_colcount, ll_colindex
    string ls_colname, ls_coltype, ls_value, ls_lineval
    int li_filenum
    any la_anyval
    
    ll_rows = adw_dw.rowcount()
    ll_colcount = long(adw_dw.Describe("DataWindow.Column.Count"))
    li_filenum = FileOpen(as_filename, LineMode!, Write!, LockWrite!, Append!)
    if li_filenum  = -1 or isnull(li_filenum) = true then 
        return -1
    end if
    for ll_row = 1 to ll_rows
        ls_lineval = ''
        for ll_colindex = 1 to ll_colcount
            ls_colname = adw_dw.describe("#" + string(ll_colindex) + ".Name")
            ls_coltype = adw_dw.Describe ( ls_colname + ".ColType" )
            CHOOSE CASE Lower ( Left ( ls_coltype , 5 ) )
                    CASE "char(", "char","strin"        //  CHARACTER DATATYPE
                        la_anyval = adw_dw.GetItemString ( ll_row, ls_colname ) 
                    CASE "date"                 //  DATE DATATYPE
                        la_anyval = adw_dw.GetItemDate ( ll_row, ls_colname ) 
                    CASE "datet"                //  DATETIME DATATYPE
                        la_anyval = adw_dw.GetItemDateTime ( ll_row, ls_colname ) 
                    CASE "decim"                //  DECIMAL DATATYPE
                        la_anyval = adw_dw.GetItemDecimal ( ll_row, ls_colname ) 
                    CASE "numbe", "long", "ulong", "real", "int"                //  NUMBER DATATYPE 
                        la_anyval = adw_dw.GetItemNumber ( ll_row, ls_colname ) 
                    CASE "time", "times"        //  TIME DATATYPE
                        la_anyval = adw_dw.GetItemTime ( ll_row, ls_colname ) 
                    CASE ELSE                   
                        SetNull ( la_anyval ) 
            END CHOOSE
            ls_value = string(la_anyval)
            if trim(ls_lineval) = '' then
                ls_lineval = ls_value
            else
                ls_lineval += as_separator + ls_value
            end if
        next //columns
        FileWrite(li_filenum, ls_lineval)
    next  //rows
    
    return 1