Application Development and Automation 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: 
Read only

How to write file to application server?

Former Member
0 Likes
5,297

Hi all,

Can any one pass the sample code for writing file to Application server and Reading file from application server.

I want sample code for writing the file and reading the file from applicatioin server.

Thanks,

Balaji

Hi all,

Can any one pass the sample code for writing file to Application server and Reading file from application server.

I want sample code for writing the file and reading the file from applicatioin server.

Thanks,

Balaji

15 REPLIES 15
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,743

Here is a sample.



report zrich_0001.

parameters: d1 type localfile default '/usr/sap/TST/SYS/Test.txt'.

data: begin of itab occurs 0,
      field1(20) type c,
      field2(20) type c,
      field3(20) type c,
      end of itab.
data: str type string.

constants: con_tab type x value '09'.

* if you have a newer version, then you can use this instead.
*constants:
*    con_tab  type c value cl_abap_char_utilities=>HORIZONTAL_TAB.

start-of-selection.

itab-field1 = 'ABC'.
itab-field2 = 'DEF'.
itab-field3 = 'GHI'.
append itab.

itab-field1 = '123'.
itab-field2 = '456'.
itab-field3 = '789'.
append itab.

  open dataset d1 for output in text mode.
  loop at itab.
    concatenate itab-field1 itab-field2 itab-field2 into str
                  separated by con_tab.
    transfer str to d1.
  endloop.
  close dataset d1.

Regards,

Rich Heilman

Read only

former_member187255
Active Contributor
Read only

Former Member
0 Likes
2,743

Hi,

<b>For reading the file.</b>

DATA:

dsn(20) VALUE '/usr/test.dat',

rec(80) OCCURS 0 WITH HEADER LINE.

OPEN DATASET dsn.

IF sy-subrc = 0.

DO.

READ DATASET dsn INTO rec.

IF sy-subrc <> 0.

EXIT.

ELSE.

APPEND rec.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET dsn.

<b>For writing file</b>

DATA:

dsn(20) VALUE '/usr/test.dat',

rec(80) OCCURS 0 WITH HEADER LINE.

OPEN DATASET dsn FOR OUTPUT.

IF sy-subrc = 0.

LOOP AT REC.

  • Write the file from the internal table.

TRANSFER rec TO dsn.

ENDLOOP.

ENDIF.

CLOSE DATASET dsn.

Thanks,

Naren

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,743

Here is another which is sort of like an application server file browser, you can click on each file and it will be read and displayed in a list display, you can see how to read a file in the last part of this code.



report zrich_0001 .

data: begin of itab occurs 0,
      rec(1000) type c,
      end of itab.
data: wa(1000) type c.

data: p_file type localfile.
data: ifile type table of  salfldir with header line.

parameters: p_path type salfile-longname
                    default '/usr/sap/TST/DVEBMGS01/data/'.


call function 'RZL_READ_DIR_LOCAL'
     exporting
          name           = p_path
     tables
          file_tbl       = ifile
     exceptions
          argument_error = 1
          not_found      = 2
          others         = 3.

loop at ifile.
  format hotspot on.
  write:/ ifile-name.
  hide ifile-name.
  format hotspot off.
endloop.


at line-selection.

  concatenate p_path ifile-name into p_file.

  clear itab.  refresh itab.
  open dataset p_file for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset p_file into wa.
      if sy-subrc <> 0.
        exit.
      endif.
      itab-rec = wa.
      append itab.
    enddo.
  endif.
  close dataset p_file.

  loop at itab.
    write:/ itab.
  endloop.

Regards,

RIch Heilman

Read only

Former Member
0 Likes
2,743

Hi,

Follow following path:

Create A Logical Path On the server...

Open a DATA SEt.

Transfer u r string to Data Site.

Close Data Set.

Read only

Former Member
0 Likes
2,743

Hi,

Please check example codes from help.sap.com

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3c8c358411d1829f0000e829fbfe/frameset.htm

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
2,743

hi,

try these,Try with the following Code

&----


*& Report ZTRAINING_FILE_UPLOAD_APPL *

*& *

&----


*& *

*& *

&----


REPORT ZTRAINING_FILE_UPLOAD_APPL message-id ztraining .

----


  • types

----


**-- types for data to be downloaded

types: begin of tp_kna1,

KUNNR type kna1-kunnr, " customer number

LAND1 type kna1-land1, " Country Key

NAME1 type kna1-name1, " customer name

ORT01 type kna1-ort01, " city

PSTLZ type kna1-pstlz, " postal code

REGIO type kna1-regio, " region

end of tp_kna1.

----


  • I n t e r n a l T a b l e s

----


**-- Internal table for data to be downloaded

data: it_kna1 type standard table of tp_kna1 with header line.

----


  • S e l e c t i o n S c r e e n

----


selection-screen begin of block b1 with frame title text-001.

parameters: p_afile type rlgrap-filename

default 'customer.txt'.

selection-screen end of block b1.

----


----


  • S t a r t o f s e l e c t i o n

----


start-of-selection.

*-- Upload the data from Sales Deal file

perform upload_app_file.

end-of-selection.

perform write_data.

*&----


*& Form upload_app_file

*&----


  • Upload the application server file data

*----


form upload_app_file.

data: l_msg(100). " error message

open dataset p_afile for input in text mode encoding default message

l_msg.

if sy-subrc <> 0.

message i004 with l_msg.

else.

do.

read dataset p_afile into it_kna1.

if sy-subrc = 0.

append it_kna1.

clear it_kna1.

else.

exit.

endif.

enddo.

close dataset p_afile.

endif.

endform. " upload_app_file

&----


*& Form write_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM write_data .

loop at it_kna1.

write:/ it_kna1-kunnr,

it_kna1-land1,

it_kna1-name1,

it_kna1-ort01,

it_kna1-pstlz,

it_kna1-regio.

endloop.

ENDFORM. " write_data

__________________________________________________________________________________________________________________

Use the following code to achieve this.

DATA: lv_string TYPE string.

CONSTANTS : lc_comma TYPE char1 VALUE ','.

  • Open the file to write the output

  • GV_DSN is your file path

OPEN DATASET gv_dsn FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  • Display error if the path specified is incorrect

IF sy-subrc NE 0.

MESSAGE ID gc_msg_class

TYPE gc_error

NUMBER gc_msgno_266.

ENDIF.

  • Prepare the Column Heading for the file

CONCATENATE text-009

text-010

text-011

text-012

text-013

text-014

text-015

text-016

text-017

text-018

text-019

INTO lv_string

SEPARATED BY lc_comma .

TRANSFER lv_string TO gv_dsn.

  • Start Writing into the file

  • Internal table Git_Contract_data contains your data to * be written on the application server

LOOP AT git_contract_det INTO wa_contract_det.

CONCATENATE wa_contract_det-cust_ref

wa_contract_det-cont_acct_ref

wa_contract_det-cont_line_item_ref

wa_contract_det-sap_site_ref

wa_contract_det-user_site_ref

wa_contract_det-mprn

wa_contract_det-contract_status

wa_contract_det-selection_criteria

wa_contract_det-credit_score

wa_contract_det-pl_cont_stdate

wa_contract_det-cont_stdate

INTO lv_string

SEPARATED BY lc_comma .

TRANSFER lv_string TO gv_dsn.

ENDLOOP.

  • Close the File

CLOSE DATASET gv_dsn.

ENDFORM. " f7000_write_output

___________________________________________________________________________________________________________________

To read data from App server.

open dataset p_fleunx for input in text mode.

if sy-subrc ne 0.

write: / 'Process will be aborted.'.

exit.

endif.

  • Load internal table

do.

read dataset p_fleunx into fltab.

if sy-subrc ne 0.

exit.

endif.

append fltab.

enddo.

To write to App. server.

open dataset p_fleunx for output in text mode.

loop at fltab.

transfer fltab to p_fleunx.

endloop.

close dataset p_fleunx.

______________________________________________________________________________________________________________

First you get your data into an internal table.

Then copy the following code.

FORM write_to_file.

                                    • Setting the file name ************************

CONCATENATE 'zfiapimg_' sy-datum '.txt' INTO wa_filename.

wa_filepath = '/data/ftp/out/'.

CONCATENATE wa_filepath wa_filename INTO wa_c_filename.

                                    • Open the file for output **********************

OPEN DATASET wa_c_filename FOR OUTPUT IN TEXT MODE.

IF sy-subrc NE 0.

MESSAGE a999 WITH 'The file could not be created'.

ENDIF.

                                • Write header data to file ************************

TRANSFER header TO wa_c_filename.

                                • Write line items to file ************************

LOOP AT i_final_file.

TRANSFER i_final_file TO wa_c_filename.

ENDLOOP.

                                          • Close the file ******************************

CLOSE DATASET wa_c_filename.

ENDFORM.

You will have the contents in the file name and file path as specified.

___________________________________________________________________________________________________________________

Read only

Former Member
0 Likes
2,743

hi,

check out this code

REPORT znavneeth1_seq.

*----


*TABLES

*----


TABLES : zvh4479 ,

vbap.

DATA : i_tab1 LIKE STANDARD TABLE OF zvh4479s WITH HEADER LINE.

DATA : i_tab2 LIKE STANDARD TABLE OF zvh4479 WITH HEADER LINE.

DATA : i_tab3 LIKE STANDARD TABLE OF zvh4479s WITH HEADER LINE.

*----


*VARIABLES

*----


DATA : ds1 TYPE string VALUE 'SEQ1.DAT'.

*----


*START-OF-SELECTION

*----


SELECT *

INTO TABLE I_TAB2

FROM ZVH4479.

loop at i_tab2.

move-corresponding i_tab2 to i_tab1.

append i_tab1.

endloop.

IF sy-subrc NE 0.

MESSAGE 'NO HEADER DATA' TYPE 'E'.

ENDIF.

*----


*DATASET OPERATIONS

*----


OPEN DATASET ds1

FOR OUTPUT

IN TEXT MODE ENCODING NON-UNICODE.

IF sy-subrc EQ 0.

LOOP AT i_tab1.

TRANSFER i_tab1 TO ds1.

ENDLOOP.

ELSE.

MESSAGE 'DATASET COULD NOT BE OPENED 1' TYPE 'E'.

ENDIF.

CLOSE DATASET ds1.

OPEN DATASET ds1

FOR INPUT

IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

BREAK-POINT.

DO.

READ DATASET ds1 INTO i_tab3.

IF sy-subrc EQ 4.

EXIT.

ENDIF.

APPEND i_tab3.

CLEAR i_tab3.

ENDDO.

ELSE.

MESSAGE 'DATASET COULD NOT BE OPENED 2' TYPE 'E'.

ENDIF.

CLOSE DATASET ds1.

*----


*END-OF-SELECTION

*----


WRITE :/1 'INTRENAL TABLE 1'.

LOOP AT i_tab1.

WRITE :/1 i_tab1-vbeln ,

10 i_tab1-erdat ,

20 i_tab1-ernam ,

30 i_tab1-lifnr ,

40 i_tab1-name1 ,

50 i_tab1-netwr.

ENDLOOP.

WRITE :/1 'INTRENAL TABLE 3'.

LOOP AT i_tab3.

WRITE :/1 i_tab3-vbeln ,

10 i_tab3-erdat ,

20 i_tab3-ernam ,

30 i_tab3-lifnr ,

40 i_tab3-name1 ,

50 i_tab3-netwr.

ENDLOOP.

the table declard here may not be present in your system just replace it with any of your tables

reward points if helpfull

Read only

Former Member
0 Likes
2,743

Balaji,

See the simple example.

DATA : V_COUNTR LIKE EDIDS-COUNTR,

FNAME(255) TYPE C VALUE '/data/sapdata/eu_ums/astdetails'.

CONSTANTS : C_COMMA TYPE C VALUE ','.

Column Headings for output.

-


I_FINAL-DOCNUM = 'IDOCNO'.

I_FINAL-STATXT = 'ERROR TEXT'.

I_FINAL-STAPA1 = 'ERROR DETAILS'.

I_FINAL-QMNUM = 'NOTIFICATION'.

I_FINAL-ZASSETCLASS = 'ASSETCLASS'.

I_FINAL-ZDATACODE = 'DATACODE'.

I_FINAL-ZMFRCODE = 'MANUFACTURER'.

I_FINAL-ZMODELCODE = 'MODELCODE'.

I_FINAL-ZSERNO = 'SERIALNO'.

*

INSERT I_FINAL INDEX 1.

CLEAR I_FINAL.

Moving the records to file

-


LOOP AT I_FINAL.

CONCATENATE

I_FINAL-DOCNUM

I_FINAL-STATXT

I_FINAL-STAPA1

I_FINAL-QMNUM

I_FINAL-ZASSETCLASS

I_FINAL-ZDATACODE

I_FINAL-ZMFRCODE

I_FINAL-ZMODELCODE

I_FINAL-ZSERNO

into I_DISPLAY-REC SEPARATED BY C_COMMA.

APPEND I_DISPLAY.

CLEAR I_DISPLAY.

ENDLOOP.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE.

LOOP AT I_DISPLAY.

TRANSFER I_DISPLAY TO FNAME.

ENDLOOP.

CLOSE DATASET FNAME.

*******************************

Pls. Reward for all useful answers.

Read only

Former Member
0 Likes
2,743

Hi,

Below is my code for writing file to application server.I am getting the error as below 'System tried to access the file 'E:\usr\sap\PMG\D00\data' but it is not opened.Open the file before accessing.

DATA: d1(80) type C value 'E:\usr\sap\PMG\D00\data'.

data: begin of itab occurs 0,

field1(20) type c,

field2(20) type c,

field3(20) type c,

end of itab.

data: str type string.

*constants: con_tab type x value '09'.

*start-of-selection.

itab-field1 = 'ABC'.

itab-field2 = 'DEF'.

itab-field3 = 'GHI'.

append itab.

itab-field1 = '123'.

itab-field2 = '456'.

itab-field3 = '789'.

append itab.

open dataset d1 for output in text mode encoding default.

loop at itab.

concatenate itab-field1 itab-field2 itab-field2 into str

separated by space.

transfer str to d1.

endloop.

Can you any one please suggest me what might be the error.This path 'E:\usr\sap\PMG\D00\data' is there in AL11.How to place file in that location and what should be the extension for that file.

Thanks,

Balaji

Read only

0 Likes
2,743

try specifying a filename instead of a directory name

use <b>E:\usr\sap\PMG\D00\data.txt</b> instead of E:\usr\sap\PMG\D00\data

Read only

0 Likes
2,743

hi every one..

it was really great code..but i've a question regarding the same so why i did not open the new thread. My question is how do we come to know whether the data from the internal table is successful transferred to the DSN on the application server..plz tell me except using sy-subrc or the tcode AL11.

thanks,

shamim

Read only

0 Likes
2,743

use transaction CG3Y to transfer the file to your presentation server and then view the file to see if the data in it is correct

Read only

Former Member
0 Likes
2,743

Hi,

Please try to declare like this.

DATA: d1(80) type C value '/usr/sap/PMG/D00/data'.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
2,743

This works for us:

DATA: dsn(40) VALUE '
sapdev1\Filename.txt'.

DATA: wa_itab like line of itab.

OPEN DATASET dsn FOR APPENDING IN TEXT MODE.

IF SY-SUBRC = 0.

LOOP AT ITAB INTO wa_itab.

<< code removed>>

TRANSFER wa_itab TO '
sapdev1\Filename.txt'.

ENDLOOP.

ENDIF.

CLOSE DATASET dsn.

Reward if helpful.