ABAP Quick - Backup for reports
Save your own reports locally with one click? In this article, we'll show you how to do that without much effort.
Table of contents
You need a backup of your previous programs/reports you wrote? With today's tip, it's no longer a problem to extract everything with one click and archive it if necessary. We'll show you an efficient report that does it all for you.
Data definition
In the first step we define the data structure for the preparation of the data to be read. For this we need a table that contains the source code after reading. Since this is a simple report, the variables are directly declared globally.
" Types
TYPES:
BEGIN OF ts_data,
objnr TYPE versobjnam,
name TYPE string,
path TYPE string,
END OF ts_data,
tt_data TYPE STANDARD TABLE OF ts_data.
" Data
DATA:
gt_text TYPE STANDARD TABLE OF abaptxt255,
gw_text TYPE abaptxt255,
gt_data TYPE tt_data,
gw_data TYPE ts_data.
Selection screen
As second step we define the selection screen with the storage parameters. For which user should the data be extracted (in this case the own user) and in which local folder the finished files should be backed up.
" Define Parameters
PARAMETERS:
p_user TYPE syuname DEFAULT sy-uname,
p_path TYPE string LOWER CASE DEFAULT 'C: empcode',
p_test AS CHECKBOX DEFAULT abap_true.
Read and prepare
You use the table TRDIR to read in all includes and reports and prepare them in the data structure. As a result, you get all the ingredients in an internal table. Of course, you can also save this step and start directly with the preparation and output of the source code to the files.
" Get data
SELECT *
FROM trdir
WHERE cnam = @p_user
AND ( name LIKE 'LZ%' OR name LIKE 'SAPLZ%' OR name LIKE 'Z%' ).
INTO TABLE @DATA(gt_dir).
" Fill structure
LOOP AT gt_dir INTO DATA(gs_dir).
CLEAR gw_data.
gw_data-objnr = gs_dir-name.
gw_data-name = gs_dir-name.
gw_data-path = p_path && gw_data-name && '.txt'.
APPEND gw_data TO gt_data.
ENDLOOP.
Download and print
Now you process the data from the global table and read in the content of the resource via the READ REPORT command. The test flag determines whether the content should only be output or the files should be stored on the file system.
LOOP AT gt_data INTO gw_data.
" Clear data
CLEAR: gt_text.
" Get the code
READ REPORT gw_data-objnr INTO gt_text.
IF sy-subrc <> 0.
WRITE: / 'Error: ', gw_data-name.
CONTINUE.
ENDIF.
" Test mode -> only some info
IF p_test = abap_true.
WRITE: / 'File found: ', gw_data-path.
CONTINUE.
ENDIF.
" Download as text
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = gw_data-path
write_lf = 'X'
CHANGING
data_tab = gt_text
EXCEPTIONS
OTHERS = 1
).
IF sy-subrc = 0.
WRITE: / 'File written: ', gw_data-path.
ENDIF.
ENDLOOP.
Conclusion
The reading of the source code is implemented with nearly no effort, as well as the download to the local computer. Thus, the report is a simple tool to help you manage your own source code and bring it in file form. Likewise, old reports can be archived and stored.