Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

delimiter in txt file

Former Member
0 Kudos
836

Hi,

Im creating a txt file using dataset in the application server and transferring data through internal table.

How to get a TAB or delimiter between each field value in the txt file?

Currently all the fileds are transferred without TAB or delimiter.

RR

10 REPLIES 10

Former Member
0 Kudos
238

hi,

You can use CONCATENATE statement.

like

CONCATENATE

text-001

text-002

text-003

text-004

text-005

text-006

text-007

text-008

text-009

text-010

text-011

text-012

INTO it_attach SEPARATED BY ' l ' .

append it_attach.

and then download this on application server using open data set and close dataset statements

thanks

former_member242255
Active Contributor
0 Kudos
238

write as below..

CONCATENATE <all the fieds> SEPARATED by COMMA or <delimiter> into <Variable>,,

and then transfer to the file....

Former Member
0 Kudos
238

Hi,

If you are creating a textfile on application server then use,

concataate workarea-name nto workarea-name.

Transfer workarea to filename.

Thanks & Regards,

Ruchi Tiwari

Former Member
0 Kudos
238

Hi Rajiv,

You can use following code to achieve your goal. By using this logic you dont need to write each field name in the concatenate statement. The delimiter of your choice will get appended at runtime.


*Loop across the table received as input e.g input_tab

LOOP AT input_tab.

* Clear the temporary string
  CLEAR l_string.
  
  DO.
* Get name of next structure field into <fs>   
    ASSIGN COMPONENT sy-index OF STRUCTURE input_tab TO <fs>.
* If no more fields in structure, exit the DO-ENDO loop
    IF sy-subrc <> 0.
      EXIT.
    ELSE.
* First convert it to charcter format so that CONCATENATE works
      l_char = <fs>.
      IF l_char IS INITIAL.
       l_char = '""'.  "You can use this statement if there are no contents in the field , else you can remove this statement.
      ENDIF.
    ENDIF.

    IF sy-index = 1.
      l_string = l_char.
    ELSE.
* If its not the first column then add a tab.
* You can use any other delimiter as well instead of ','  eg. '|' etc.
      CONCATENATE l_string ','
                  l_char INTO l_string.
    ENDIF.
*   Get rid of the spaces
    CONDENSE l_string.
  ENDDO.
* Append the internal table and clear the header
  MOVE l_string TO row.
  TRANSFER row TO application_server_filename.
ENDLOOP.

Hope this helps.

Thanks

Ashwani

Former Member
0 Kudos
238

Hi,

Simplest is to use :-

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

EXPORTING

i_field_seperator = ','

TABLES

i_tab_sap_data = it_final

CHANGING

i_tab_converted_data = w_data

EXCEPTIONS

conversion_failed = 1

OTHERS = 2.

I hope this helps,

Regards

Raju Chitale

Former Member
0 Kudos
238

hi,

try this..



CONCATENATE <field name1 > <field name2>.......into <variable name> SEPERATED By  <DELIMITER> .

hope this help you

Regards

Ritesh J

Former Member
0 Kudos
238

Hi Rajiv,

PARAMETERS:
 p_fname LIKE dxfields-longpath OBLIGATORY,
 p_file1 LIKE IBIPPARMS-PATH OBLIGATORY.

DATA:
  BEGIN OF fs_bkpf,
    bukrs LIKE bkpf-bukrs,
    belnr LIKE bkpf-belnr,
    gjahr LIKE bkpf-gjahr,
    blart LIKE bkpf-blart,
    bldat LIKE bkpf-bldat,
    budat LIKE bkpf-budat,
    monat LIKE bkpf-monat,
    cpudt LIKE bkpf-cpudt,
  END OF fs_bkpf.

DATA:
  t_bkpf LIKE
STANDARD TABLE
      OF fs_bkpf.

DATA:
  w_line TYPE i,
  w_file1 like rlgrap-filename,
  w_file TYPE string,
  W_STR LIKE FS_BKPF.

SELECT-OPTIONS:
  s_bukrs FOR fs_bkpf-bukrs,
  s_bldat FOR fs_bkpf-bldat.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
 IMPORTING
   O_PATH                = p_fname
 EXCEPTIONS
   RFC_ERROR             = 1
   ERROR_WITH_GUI        = 2
   OTHERS                = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE1.

CALL FUNCTION 'F4_FILENAME'
 IMPORTING
   FILE_NAME           = P_FILE1.

start-of-selection.

move p_file1 to w_file1.
SELECT bukrs
       belnr
       gjahr
       blart
       bldat
       budat
       monat
       cpudt
  FROM bkpf
  INTO TABLE t_bkpf
 WHERE bukrs IN s_bukrs
   AND bldat IN s_bldat.
DESCRIBE TABLE t_bkpf LINES w_line.

OPEN DATASET p_fname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT t_bkpf INTO fs_bkpf.
  CONCATENATE fs_bkpf-bukrs
              fs_bkpf-belnr
              fs_bkpf-gjahr
              fs_bkpf-blart
              fs_bkpf-bldat
              fs_bkpf-budat
              fs_bkpf-monat
              fs_bkpf-cpudt
  INTO W_STR SEPARATED BY space.
  TRANSFER fs_bkpf TO p_fname.
ENDLOOP.

CLOSE DATASET p_fname.

REFRESH t_bkpf.

OPEN DATASET p_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc <> 0.
  EXIT.
ELSE.
  DO.
    READ DATASET p_fname INTO fs_bkpf.
    APPEND fs_bkpf TO t_bkpf.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.
  ENDDO.
ENDIF.

MOVE w_file1 TO w_file.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename                      = w_file
   filetype                      = 'ASC'
   write_field_separator         = 'X'
  TABLES
    data_tab                      = t_bkpf
 EXCEPTIONS
   FILE_WRITE_ERROR              = 1
   NO_BATCH                      = 2
   GUI_REFUSE_FILETRANSFER       = 3
   INVALID_TYPE                  = 4
   NO_AUTHORITY                  = 5
   UNKNOWN_ERROR                 = 6
   HEADER_NOT_ALLOWED            = 7
   SEPARATOR_NOT_ALLOWED         = 8
   FILESIZE_NOT_ALLOWED          = 9
   HEADER_TOO_LONG               = 10
   DP_ERROR_CREATE               = 11
   DP_ERROR_SEND                 = 12
   DP_ERROR_WRITE                = 13
   UNKNOWN_DP_ERROR              = 14
   ACCESS_DENIED                 = 15
   DP_OUT_OF_MEMORY              = 16
   DISK_FULL                     = 17
   DP_TIMEOUT                    = 18
   FILE_NOT_FOUND                = 19
   DATAPROVIDER_EXCEPTION        = 20
   CONTROL_FLUSH_ERROR           = 21
   OTHERS                        = 22
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

Regards,

Sravanthi

Former Member
0 Kudos
238

Hi Rajiv,

Try this...

data: tabstop(2) type c.

tabstop = cl_abap_char_utilities=>horizontal_tab.

concatenate field1 field2 into w_text separated by tabstop.

transfer w_text to xxxxxx

--

Raghu

Former Member
0 Kudos
238

Hi Rajiv,

As also said by others do it this way:

DATA:
  w_fname(60) VALUE                  " File Name on Application Ser
              '.\file.txt',
  w_string TYPE string,                " String Variable
  w_char   TYPE c VALUE ' '.       " Variable for Field Separator

  OPEN DATASET w_fname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  LOOP AT itab INTO wa_itab.

    CONCATENATE wa_itab-field1
                wa_itab-field2
                wa_itab-field3
                wa_itab-field4
                wa_itab-field5
           INTO w_string SEPARATED BY w_char.

    TRANSFER w_string TO w_fname.

  ENDLOOP.                        
  IF sy-subrc EQ 0.
    message s001(class) with w_fname.
  ENDIF.                               " IF SY-SUBRC EQ 0

  CLOSE DATASET w_fname.

With luck,

Pritam.

Former Member
0 Kudos
238

g