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: 

How can we create a file from ABAP?

Former Member
0 Kudos

Hi All,

Can anybody please tell me how we can create a file in the presentation server from ABAP?

Thanks in advance,

Regards,

Bijesh

1 ACCEPTED SOLUTION

Former Member
0 Kudos

u need to get data into final internal table...

in a variable specify the file path of the presentation server...

now use GUI_DOWNLOAD to create the file with contents...

8 REPLIES 8

Former Member
0 Kudos

Hi,

Use GUI_DOWnload.. Here is a program for your reference.

DATA : LV_FNAME TYPE STRING.

select * from vbap into table itab.

LV_FNAME = 'c:\testfile.csv'.

*-- Download the Error Records into a File.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

FILENAME = LV_FNAME

TABLES

DATA_TAB = I_tab

EXCEPTIONS

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.

Thanks

Mahesh

Former Member
0 Kudos

ABAP code to create and write to a file:

  • Check whether file already exists

OPEN DATASET g_out_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc IS INITIAL.

  • File g_out_file already exists. Do not write to an existing file

  • Error message

STOP.

ENDIF.

  • open outbound file for writing

OPEN DATASET g_out_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  • write detail record to outbound file

LOOP AT t_iout INTO g_out.

TRANSFER g_out TO g_out_file.

ENDLOOP.

CLOSE DATASET g_out_file.

IF sy-subrc IS INITIAL.

  • Success message

ENDIF.

CATCH cx_root INTO l_oref.

l_text = l_oref->get_text( ).

  • log free text message if exception is catched

  • Error message

STOP.

ENDTRY.

Assign pts if its useful

Former Member
0 Kudos

u need to get data into final internal table...

in a variable specify the file path of the presentation server...

now use GUI_DOWNLOAD to create the file with contents...

Former Member
0 Kudos

Hi Krishman,

Have a look at class cl_gui_frontend_services (4.7 forward), there you will find a lot of methos do work with files on presentation server. Inclusive upload and download.

Otherwise you can use:

open dataset filename for ouput in text mode.

transfer line to filename.

close dataset filename.

Use F1 for detail information....

But using this open dataset...directory where to write has to be able to reache from network (Shared with write permission).

Regards,

Marcelo Moreira

0 Kudos

Hi Marcelo,

i could do it using the GUI_DOWNLOAD. but i would like to know if there is any static method available in the CL_GUI_FRONTEND_SERVICES class for creating a file.

i checked but could not find any. Please mention if u are aware of any method.

Regards,

Bijesh

0 Kudos

Hi Bijesh,

I guess there is no static method , you can use the same method GUI_DOWNLOAD in that class

0 Kudos

Hi Bijesh,

At the moment I only have 4.6C version and this version there a less methods. In which version are you?

Maybe, there is no explicit method like p.e. file_create, but what about the method:

FILE_SAVE_DIALOG

OR

FILE_SAVE (I am not absolutly sure if there is this methos, in 4.6C it does not)

Doesn´t you get what you want with these method's?

Regards,

Marcelo

Former Member
0 Kudos

Hi,

You can use DOWNLOAD OR WS_DOWNLOAD OR GUI_DOWNLOAD function modules.

Ex.

Suppose the presentation server is running under Windows NT, and you have written

the following program:

PROGRAM SAPMZTST.

DATA: FLENGTH TYPE I.

DATA TAB(80) OCCURS 5.

APPEND 'This is the first line of my text. ' TO TAB.

APPEND 'The second line. ' TO TAB.

APPEND ' The third line. ' TO TAB.

APPEND ' The fourth line. ' TO TAB.

APPEND ' Fifth and final line. ' TO TAB.

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

CODEPAGE = 'IBM'

FILENAME = 'd:\temp\saptest.txt'

FILETYPE = 'ASC'

IMPORTING

FILELENGTH = FLENGTH

TABLES

DATA_TAB = TAB

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_WRITE_ERROR = 2

INVALID_FILESIZE = 3

INVALID_TABLE_WIDTH = 4

INVALID_TYPE = 5.

WRITE: 'SY-SUBRC :', SY-SUBRC,

/ 'File length:', FLENGTH.

The output appears as follows:

SY-SUBRC : 0

File length: 151

The system has written the five lines of the table TAB into the ASCII file

D:\temp\saptest.txt.

Regards,

Bhaskar