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: 

write file to server

Former Member
0 Kudos
156

Hallow

here in my code I read file from server and its working well

Now I wont write the file in itab to different place in server (ex. 😧
program
2)

How I do that?

regards

OPEN DATASET file_ser IN TEXT MODE

ENCODING DEFAULT FOR INPUT.

IF sy-subrc NE 0.

no_file = 'X'.

ELSE.

DO.

READ DATASET file_ser INTO irec.

IF sy-subrc NE 0.

EXIT.

ENDIF.

APPEND irec TO itab.

ENDDO.

ENDIF.

CLOSE DATASET file_ser.

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
113

This example, shows both, it reads in a file from the application server, and then writes the contents to another file name on the application server. So in reality it is a "Copying" program, you can change the file paths to what ever you want.



report zrich_0001.

Parameters: d1 type localfile default '/usr/sap/TST/SYS/Data1.txt',
            d2 type localfile default '/usr/sap/TST/SYS/Data2.txt'.

data: itab type table of string.
data: wa type string.


start-of-selection.

* Read file
  open dataset d1 for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset d1 into wa.
      if sy-subrc <> 0.
        exit.
      endif.
      append wa to itab.
    enddo.
  endif.
  close dataset d1.


* Copy to new file
  open dataset d2 for output in text mode.
  loop at itab into wa.
    transfer wa to d2.
  endloop.
  close dataset d2.



Regards,

Rich Heilman

6 REPLIES 6

Former Member
0 Kudos
113

Hi antonio,

1. This kind of logic.

2. OPEN DATA 'xyz' for INPUT

loop at itab.

TRANSFER ITAB to 'xyz'.

ENDLOOP.

close dataset.

regards,

amit m.

Former Member
0 Kudos
113

hi,

try the following example

DATA file TYPE string VALUE `/usr/test.Z`.

OPEN DATASET file FOR OUTPUT IN BINARY MODE

FILTER 'compress'.

...

CLOSE DATASET file.

regards,

Navneeth.K

Former Member
0 Kudos
113

Hi,

Check these links,

Believe that it will work.

Regards,

-->Suresh

Former Member
0 Kudos
113

Hi,

Try the following code n let me know.

*"Table declarations...................................................

tables: kna1.

*"Selection screen elements............................................

select-options: s_kunnr for kna1-kunnr. " Customer Number 1

"----


  • Type declaration of the structure to hold Customer master *

"----


data:

begin of fs_kna1,

kunnr type kna1-kunnr, " Customer Number 1

adrnr type kna1-adrnr, " Address

anred type kna1-anred, " Title

erdat type kna1-erdat, " Date on which Record Was reated

ernam type kna1-ernam, " Name of Person who Created

end of fs_kna1.

"----


  • Internal table to hold Customer master *

"----


data:

t_kna1 like standard table

of fs_kna1.

"----


  • Type declaration of the structure to hold file data *

"----


data:

begin of fs_table,

str type string,

end of fs_table.

"----


  • Internal table to hold file data *

"----


data:

t_table like standard table

of fs_table.

field-symbols: <fs>.

*" Data declarations...................................................

"----


  • Work variables *

"----


data:

w_char(50) type c.

select kunnr " Customer Number 1

adrnr " Address

anred " Title

erdat " Date on which Record Was reated

ernam " Name of Person who Created

from kna1

into table t_kna1

where kunnr in s_kunnr.

if sy-subrc eq 0.

loop at t_kna1 into fs_kna1.

do.

assign component sy-index of structure fs_kna1 to <fs>.

if sy-subrc ne 0.

exit.

else.

move <fs> to w_char.

if sy-index eq 1.

fs_table-str = <fs>.

else.

concatenate fs_table-str ',' w_char into fs_table-str.

endif. " IF SY-INDEX...

endif. " IF SY-SUBRC...

enddo. " DO...

append fs_table to t_table.

endloop. " LOOP AT T_KNA1...

endif. " IF SY-SUBRC...

call function 'GUI_DOWNLOAD'

exporting

  • BIN_FILESIZE =

filename = 'C:\Assign\kna1'

  • FILETYPE = 'ASC'

  • APPEND = ' '

  • WRITE_FIELD_SEPARATOR = ' '

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • CONFIRM_OVERWRITE = ' '

  • NO_AUTH_CHECK = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • WRITE_BOM = ' '

  • TRUNC_TRAILING_BLANKS_EOL = 'X'

  • WK1_N_FORMAT = ' '

  • WK1_N_SIZE = ' '

  • WK1_T_FORMAT = ' '

  • WK1_T_SIZE = ' '

  • IMPORTING

  • FILELENGTH =

tables

data_tab = t_table

  • FIELDNAMES =

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,

kiran kumar k

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
114

This example, shows both, it reads in a file from the application server, and then writes the contents to another file name on the application server. So in reality it is a "Copying" program, you can change the file paths to what ever you want.



report zrich_0001.

Parameters: d1 type localfile default '/usr/sap/TST/SYS/Data1.txt',
            d2 type localfile default '/usr/sap/TST/SYS/Data2.txt'.

data: itab type table of string.
data: wa type string.


start-of-selection.

* Read file
  open dataset d1 for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset d1 into wa.
      if sy-subrc <> 0.
        exit.
      endif.
      append wa to itab.
    enddo.
  endif.
  close dataset d1.


* Copy to new file
  open dataset d2 for output in text mode.
  loop at itab into wa.
    transfer wa to d2.
  endloop.
  close dataset d2.



Regards,

Rich Heilman

Former Member
0 Kudos
113

Hi,

peudocode:

1st create a internal table (itab_path) which will have all the file path store in it.

LOOP at itab_path into wa_path

CONCATENATE wa_path c_slash l_filename INTO v_fpath.

OPEN DATASET v_fpath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa_itab.

CONCATENATE wa_itab-field1

wa_itab-field2

wa_itab-field3

INTO l_file

SEPARATED BY

c_tab.

TRANSFER l_file TO v_fpath.

ENDLOOP.

ENDIF.

CLOSE DATASET v_fpath.

ENDLOOP.

Reward is useful,

Regards,

Tanmay