Die Methoden CP / MV / REN / DIRLIST3 plus Baustein nur in DEMO Systemen implementieren !
bzw. SM69 / CALL FUNCTION 'SXPG_COMMAND_EXECUTE' verwenden
Code: Alles auswählen.
class ZCL_GUI_BACKEND_SERVICES definition
public
final
create public .
public section.
type-pools ABAP .
type-pools CNDP .
type-pools SABC .
class-methods FILE_EXIST
importing
value(FILE) type STRING
returning
value(RESULT) type ABAP_BOOL
exceptions
WRONG_PARAMETER
NO_AUTHORITY
SOURCE_NOT_EXISTS
NO_PERMISSION
OPEN_FAILED .
class-methods FILE_GET_SIZE
importing
value(FILE) type STRING
returning
value(FILE_SIZE) type INT4
exceptions
WRONG_PARAMETER
NO_AUTHORITY .
class-methods CHECK_AUTHORITY
importing
value(I_FILE) type STRING
value(ACTIVITY) type CHAR20
returning
value(RC) type SYST-SUBRC
exceptions
NO_PERMISSION .
class-methods GET_FILE_SEPARATOR
returning
value(SEP) type CHAR01 .
class-methods FILE_READ_BIN
importing
!FILE type STRING
exporting
!FILESIZE type I
returning
value(DATA) type SOLIX_TAB
exceptions
NO_PERMISSION
OPEN_FAILED
READ_ERROR
PATH_ERROR .
class-methods FILE_WRITE_BIN
importing
!DATA type SOLIX_TAB
!FILE type STRING
!I_FILE_OVERWRITE type ABAP_BOOL default ' '
!I_FILE_SIZE type I default 0
exceptions
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS
CLOSE_FAILED
WRITE_FAILED .
class-methods FILE_WRITE_BIN2
importing
!DATA type STANDARD TABLE
!FILE type STRING
!I_FILE_OVERWRITE type ABAP_BOOL default ' '
!I_FILE_SIZE type I default 0
exceptions
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS
CLOSE_FAILED
WRITE_FAILED .
class-methods FILE_DELETE
importing
!FILE type STRING
exceptions
NO_PERMISSION
FAILURE .
class-methods DIRECTORY2_LIST
importing
value(I_PATH) type STRING
!I_MASK type STRING default '*'
changing
value(C_DIR_NAME) type EPSF-EPSDIRNAM optional
value(C_FILE_COUNTER) type EPSF-EPSFILSIZ optional
value(C_ERROR_COUNTER) type EPSF-EPSFILSIZ optional
returning
value(DIRLIST) type ZCSTT_EPS2FILI
exceptions
WRONG_PARAMETER
INVALID_EPS_SUBDIR
SAPGPARAM_FAILED
BUILD_DIRECTORY_FAILED
NO_AUTHORIZATION
READ_DIRECTORY_FAILED
TOO_MANY_READ_ERRORS
EMPTY_DIRECTORY_LIST .
class-methods DIRECTORY3_LIST
importing
value(I_PATH) type STRING
!I_MASK type STRING optional
changing
value(C_DIR_NAME) type EPSF-EPSDIRNAM optional
value(C_FILE_COUNTER) type EPSF-EPSFILSIZ optional
value(C_ERROR_COUNTER) type EPSF-EPSFILSIZ optional
returning
value(DIRLIST) type ZCSTT_EPS2FILI
exceptions
NOT_FOUND
ACCESS_FAILURE
WRONG_PARAMETER .
class-methods DIRECTORY_LIST
importing
value(I_PATH) type STRING
!I_MASK type STRING default '*'
returning
value(DIRLIST) type ZCSTT_EPSFILI
exceptions
WRONG_PARAMETER
INVALID_EPS_SUBDIR
SAPGPARAM_FAILED
BUILD_DIRECTORY_FAILED
NO_AUTHORIZATION
READ_DIRECTORY_FAILED
TOO_MANY_READ_ERRORS
EMPTY_DIRECTORY_LIST .
class-methods SPLIT_FILENAME
importing
!FILE type STRING
exporting
!PATH type STRING
!FILE_WITH_EXT type STRING
!EXT type STRING .
class-methods FILE_APPEND_TEXT
importing
value(I_FILE) type STRING optional
value(I_LANGU) type SY-LANGU default SY-LANGU
exporting
!E_ERROR_TAB type BAPIRET2_TAB
changing
!I_TEXTDATA_TAB type STANDARD TABLE
exceptions
NO_PERMISSION
OPEN_FAILED .
class-methods FILE_WRITE_TEXT
importing
value(I_FILE) type STRING optional
value(I_FILE_OVERWRITE) type XFELD optional
value(I_LANGU) type SY-LANGU default SY-LANGU
exporting
!E_ERROR_TAB type BAPIRET2_TAB
changing
!I_TEXTDATA_TAB type STANDARD TABLE
exceptions
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS .
class-methods FILE_WRITE_TEXT_TEST
importing
value(I_FILE) type STRING optional
value(I_FILE_OVERWRITE) type XFELD optional
value(I_LANGU) type SY-LANGU default SY-LANGU
exporting
!E_ERROR_TAB type BAPIRET2_TAB
changing
!I_TEXTDATA_TAB type STANDARD TABLE
exceptions
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS .
class-methods DIRECTORY_GET_ALIAS_PATH
importing
value(I_ALIAS) type C
returning
value(O_PATH) type STRING
exceptions
NOT_FOUND .
class-methods CP
importing
!I_SOURCE type STRING
!I_DEST type STRING
!I_FILE type STRING
returning
value(RC) type SY-SUBRC .
class-methods FILE_COPY
importing
!I_SOURCE type STRING
!I_DEST type STRING
exceptions
SOURCE_NOT_EXISTS
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS
CLOSE_FAILED
WRITE_FAILED
READ_ERROR
PATH_ERROR .
class-methods MV
importing
!I_SOURCE type STRING
!I_DEST type STRING
!I_FILE type STRING
returning
value(RC) type SY-SUBRC .
class-methods REN
importing
!I_SOURCE type STRING
!I_DEST type STRING
!I_FILE1 type STRING
!I_FILE2 type STRING
returning
value(RC) type SY-SUBRC .
class-methods FILE_MOVE
importing
!I_SOURCE type STRING
!I_DEST type STRING
exceptions
SOURCE_NOT_EXISTS
NO_PERMISSION
OPEN_FAILED
AP_FILE_EXISTS
CLOSE_FAILED
WRITE_FAILED
READ_ERROR
PATH_ERROR .
class-methods FILE_READ_TEXT
importing
!I_FILE type STRING
value(I_SEP) type CHAR01 optional
changing
!I_TEXTDATA_TAB type STANDARD TABLE
exceptions
ERROR_OPENING_FILE .
class-methods CHECK_AUTHORITY_SYSCMD
exceptions
NOT_ALLOWED
NO_OS_COMMANDS_ALLOWED .
class-methods CONVERT2CSV
importing
!IN type STANDARD TABLE
value(SEP) type CHAR01 default %_HORIZONTAL_TAB
exporting
value(OUT) type STANDARD TABLE .
PROTECTED SECTION.
PRIVATE SECTION.
CONSTANTS lg_max_len TYPE i VALUE 255 ##NO_TEXT.
ENDCLASS.
CLASS ZCL_GUI_BACKEND_SERVICES IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>CHECK_AUTHORITY
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_FILE TYPE STRING
* | [--->] ACTIVITY TYPE CHAR20
* | [<-()] RC TYPE SYST-SUBRC
* | [EXC!] NO_PERMISSION
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD check_authority.
* CONSTANTS:
* SABC_ACT_READ(4) VALUE 'READ',
* SABC_ACT_WRITE(5) VALUE 'WRITE',
* SABC_ACT_READ_WITH_FILTER(16) VALUE 'READ_WITH_FILTER',
* SABC_ACT_WRITE_WITH_FILTER(17) VALUE 'WRITE_WITH_FILTER',
* SABC_ACT_DELETE(6) VALUE 'DELETE',
* SABC_ACT_INIT(4) VALUE 'INIT',
* SABC_ACT_ACCEPT(6) VALUE 'ACCEPT',
* SABC_ACT_CALL(4) VALUE 'CALL'.
DATA l_file TYPE authb-filename.
l_file = i_file.
* check the authority for file
CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
EXPORTING
* PROGRAM =
activity = activity
filename = l_file
EXCEPTIONS
no_authority = 1
activity_unknown = 2
OTHERS = 3.
rc = sy-subrc.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>CHECK_AUTHORITY_SYSCMD
* +-------------------------------------------------------------------------------------------------+
* | [EXC!] NOT_ALLOWED
* | [EXC!] NO_OS_COMMANDS_ALLOWED
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD check_authority_syscmd.
*data: TCODE type TSTC-TCODE,
* USER(6),
* CMD(254), "Max length = 254 Bytes
* CMD_NR TYPE I,
* CMD_LINE TYPE I,
*
* RESULT(255) OCCURS 100 WITH HEADER LINE, "No max length
*
* BEGIN OF LIST OCCURS 100,
* NR TYPE I,
* LINE(255),
* END OF LIST,
DATA rdisp_call_system.
DATA user(6).
* HOME(60),
* opsysgroup like opsystem-filesys,
* CD_PATH(60),
* lCD_PATH(60).
* Authority check
DATA authrc TYPE i.
CALL FUNCTION 'AUTHORITY_CHECK_TCODE'
EXPORTING
tcode = 'SM69'
EXCEPTIONS
ok = 0
not_ok = 2
OTHERS = 3.
authrc = sy-subrc.
IF authrc = 0.
CALL FUNCTION 'AUTHORITY_CHECK_TCODE'
EXPORTING
tcode = 'SM49'
EXCEPTIONS
ok = 0
not_ok = 2
OTHERS = 3.
authrc = sy-subrc.
ENDIF.
IF authrc = 0.
AUTHORITY-CHECK OBJECT 'S_RZL_ADM'
ID 'ACTVT' FIELD '01'.
authrc = sy-subrc.
ENDIF.
IF authrc = 0.
AUTHORITY-CHECK OBJECT 'S_LOG_COM'
ID 'COMMAND' FIELD 'RSBDCOS0'
ID 'OPSYSTEM' FIELD sy-opsys
ID 'HOST' FIELD sy-host.
authrc = sy-subrc.
ENDIF.
IF authrc NE 0.
RAISE not_allowed.
ENDIF.
* Are OS Commands allowed?
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'rdisp/call_system' "#ec notext
ID 'VALUE' FIELD rdisp_call_system.
IF rdisp_call_system NE '1'.
RAISE no_os_commands_allowed.
ENDIF.
** Host Userid
* USER = SY-SYSID.
* USER+3 = 'adm'. "#ec notext
* TRANSLATE USER TO LOWER CASE.
*
** Read profile parameters
* CALL 'C_SAPGALLPARAM' ID 'PAR_SUB' FIELD PAR_SUB-*SYS*.
* SORT PAR_SUB BY PNAME.
* READ TABLE PAR_SUB WITH KEY PNAME = 'DIR_HOME' BINARY SEARCH.
* IF SY-SUBRC = 0.
* IF PAR_SUB-USER_WERT IS INITIAL.
* HOME = PAR_SUB-DEFAULT_WERT.
* ELSE.
* HOME = PAR_SUB-USER_WERT.
* ENDIF.
* ENDIF.
*
** Operating system group
* clear opsysgroup.
* select single filesys from opsystem into opsysgroup
* where opsys = sy-opsys.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>CONVERT2CSV
* +-------------------------------------------------------------------------------------------------+
* | [--->] IN TYPE STANDARD TABLE
* | [--->] SEP TYPE CHAR01 (default =%_HORIZONTAL_TAB)
* | [<---] OUT TYPE STANDARD TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD convert2csv.
DATA tab TYPE TABLE OF truxs_t_text_data.
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
i_field_seperator = sep
* I_LINE_HEADER =
* I_FILENAME =
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = in
CHANGING
i_tab_converted_data = tab
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>CP
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SOURCE TYPE STRING
* | [--->] I_DEST TYPE STRING
* | [--->] I_FILE TYPE STRING
* | [<-()] RC TYPE SY-SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD cp.
DATA cmd TYPE string.
CONCATENATE i_source i_file i_dest INTO cmd SEPARATED BY space.
CALL FUNCTION 'Z_BC_RUN_CONSOLE'
EXPORTING
cmd = cmd
IMPORTING
rc = rc.
* TABLES
* RESULT =
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>DIRECTORY2_LIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_PATH TYPE STRING
* | [--->] I_MASK TYPE STRING (default ='*')
* | [<-->] C_DIR_NAME TYPE EPSF-EPSDIRNAM(optional)
* | [<-->] C_FILE_COUNTER TYPE EPSF-EPSFILSIZ(optional)
* | [<-->] C_ERROR_COUNTER TYPE EPSF-EPSFILSIZ(optional)
* | [<-()] DIRLIST TYPE ZCSTT_EPS2FILI
* | [EXC!] WRONG_PARAMETER
* | [EXC!] INVALID_EPS_SUBDIR
* | [EXC!] SAPGPARAM_FAILED
* | [EXC!] BUILD_DIRECTORY_FAILED
* | [EXC!] NO_AUTHORIZATION
* | [EXC!] READ_DIRECTORY_FAILED
* | [EXC!] TOO_MANY_READ_ERRORS
* | [EXC!] EMPTY_DIRECTORY_LIST
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD directory2_list.
DATA l_dir TYPE eps2filnam.
DATA l_filemask TYPE epsf-epsfilnam.
* check parameter, wild characters not allowed
IF i_path IS INITIAL OR i_path CA '*<>|"'.
RAISE wrong_parameter.
EXIT.
ENDIF.
l_dir = i_path.
l_filemask = i_mask.
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING'
EXPORTING
iv_dir_name = l_dir
file_mask = l_filemask
IMPORTING
dir_name = c_dir_name
file_counter = c_file_counter
error_counter = c_error_counter
TABLES
dir_list = dirlist
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
CASE sy-subrc.
WHEN 1. RAISE invalid_eps_subdir.
WHEN 2. RAISE sapgparam_failed.
WHEN 3. RAISE build_directory_failed.
WHEN 4. RAISE no_authorization.
WHEN 5. RAISE read_directory_failed.
WHEN 6. RAISE too_many_read_errors.
WHEN 7. RAISE empty_directory_list.
WHEN OTHERS.
ENDCASE.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>DIRECTORY3_LIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_PATH TYPE STRING
* | [--->] I_MASK TYPE STRING(optional)
* | [<-->] C_DIR_NAME TYPE EPSF-EPSDIRNAM(optional)
* | [<-->] C_FILE_COUNTER TYPE EPSF-EPSFILSIZ(optional)
* | [<-->] C_ERROR_COUNTER TYPE EPSF-EPSFILSIZ(optional)
* | [<-()] DIRLIST TYPE ZCSTT_EPS2FILI
* | [EXC!] NOT_FOUND
* | [EXC!] ACCESS_FAILURE
* | [EXC!] WRONG_PARAMETER
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD directory3_list.
DATA l_dir TYPE eps2filnam.
DATA l_filemask TYPE epsf-epsfilnam.
DATA t_result TYPE TABLE OF tab512.
DATA s_r TYPE tab512.
DATA s_lin TYPE eps2fili.
* check parameter, wild characters not allowed
TYPES: BEGIN OF ty_dir,
rw(10),
x1(1),
v1(1),
x2(1),
user1(6),
x3(1),
user2(6),
x4(1),
size(10),
x5(1),
date(35),
x6(1),
file(1024),
END OF ty_dir.
DATA s_rawdir TYPE ty_dir.
" IF i_path IS INITIAL OR i_path CA '*<>|"'.
IF i_path CA '*<>|"'.
RAISE wrong_parameter.
EXIT.
ENDIF.
DATA cmd TYPE string.
DATA rc TYPE sy-subrc.
CONCATENATE i_path i_mask INTO cmd .
CONCATENATE 'ls -l --full-time' cmd INTO cmd SEPARATED BY space.
CALL FUNCTION 'Z_BC_RUN_CONSOLE'
EXPORTING
cmd = cmd
IMPORTING
rc = rc
TABLES
result = t_result.
*ls Exit status:
*
*0 success
*1 minor problems (e.g., failure to access a file or directory not
* specified as a command line argument. This happens when listing a
* directory in which entries are actively being removed or renamed.)
*2 serious trouble (e.g., memory exhausted, invalid option, failure
* to access a file or directory specified as a command line argument
* or a directory loop)
CASE sy-subrc.
WHEN 1. RAISE not_found.
WHEN 2. RAISE access_failure.
WHEN OTHERS.
ENDCASE.
LOOP AT t_result INTO s_rawdir.
IF s_rawdir-rw(1) = '-'.
CLEAR s_lin.
s_lin-name = s_rawdir-file.
s_lin-size = s_rawdir-size.
" 2020-04-08 16:02:05.475649000 +0200
" 25.04.2020 17:15:11
s_lin-mtim(2) = s_rawdir-date+8(2). " day
s_lin-mtim+2(1) = '.'.
s_lin-mtim+3(2) = s_rawdir-date+5(2). " month
s_lin-mtim+5(1) = '.'.
s_lin-mtim+6(4) = s_rawdir-date(4). " year
s_lin-mtim+10(1) = ' '.
s_lin-mtim+11(2) = s_rawdir-date+11(2). " hh
s_lin-mtim+13(1) = ':'.
s_lin-mtim+14(2) = s_rawdir-date+14(2). " mm
s_lin-mtim+16(1) = ':'.
s_lin-mtim+17(2) = s_rawdir-date+17(2). " ss
s_lin-owner = s_rawdir-user1.
APPEND s_lin TO dirlist.
ENDIF.
ENDLOOP.
c_file_counter = lines( dirlist ).
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>DIRECTORY_GET_ALIAS_PATH
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_ALIAS TYPE C
* | [<-()] O_PATH TYPE STRING
* | [EXC!] NOT_FOUND
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD directory_get_alias_path.
DATA dir_trans TYPE text255.
DATA l_alias TYPE text255.
* Get Path on server from alias
l_alias = i_alias.
CALL FUNCTION 'RSPO_R_SAPGPARAM'
EXPORTING
name = l_alias
IMPORTING
value = dir_trans
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
SELECT SINGLE dirname FROM user_dir INTO dir_trans WHERE aliass EQ l_alias.
IF sy-subrc <> 0.
RAISE not_found.
ENDIF.
o_path = dir_trans.
ELSE.
o_path = dir_trans.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>DIRECTORY_LIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_PATH TYPE STRING
* | [--->] I_MASK TYPE STRING (default ='*')
* | [<-()] DIRLIST TYPE ZCSTT_EPSFILI
* | [EXC!] WRONG_PARAMETER
* | [EXC!] INVALID_EPS_SUBDIR
* | [EXC!] SAPGPARAM_FAILED
* | [EXC!] BUILD_DIRECTORY_FAILED
* | [EXC!] NO_AUTHORIZATION
* | [EXC!] READ_DIRECTORY_FAILED
* | [EXC!] TOO_MANY_READ_ERRORS
* | [EXC!] EMPTY_DIRECTORY_LIST
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD directory_list.
DATA l_dir TYPE epsf-epsdirnam.
DATA l_filemask TYPE epsf-epsfilnam.
* check parameter, wild characters not allowed
IF i_path IS INITIAL OR i_path CA '*<>|"'.
RAISE wrong_parameter.
EXIT.
ENDIF.
l_dir = i_path.
l_filemask = i_mask.
CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
EXPORTING
dir_name = l_dir
file_mask = l_filemask
TABLES
dir_list = dirlist
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
CASE sy-subrc.
WHEN 1. RAISE invalid_eps_subdir.
WHEN 2. RAISE sapgparam_failed.
WHEN 3. RAISE build_directory_failed.
WHEN 4. RAISE no_authorization.
WHEN 5. RAISE read_directory_failed.
WHEN 6. RAISE too_many_read_errors.
WHEN 7. RAISE empty_directory_list.
WHEN OTHERS.
ENDCASE.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_APPEND_TEXT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_FILE TYPE STRING(optional)
* | [--->] I_LANGU TYPE SY-LANGU (default =SY-LANGU)
* | [<---] E_ERROR_TAB TYPE BAPIRET2_TAB
* | [<-->] I_TEXTDATA_TAB TYPE STANDARD TABLE
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_append_text.
* local data -----------------------------------------------------------
DATA l_filename TYPE authb-filename.
DATA ls_ret TYPE bapiret2.
FIELD-SYMBOLS <t> TYPE any.
* function body --------------------------------------------------------
* init
l_filename = i_file.
* check the authority for file
CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
EXPORTING
activity = sabc_act_write
* Authority Check allows right now only 60 Character
filename = l_filename(60)
EXCEPTIONS
no_authority = 1
activity_unknown = 2
OTHERS = 3.
IF sy-subrc <> 0.
RAISE no_permission.
ENDIF.
SET LOCALE LANGUAGE i_langu.
* open dataset for writing
OPEN DATASET i_file WITH WINDOWS LINEFEED FOR APPENDING IN TEXT MODE
ENCODING NON-UNICODE.
IF NOT sy-subrc IS INITIAL.
RAISE open_failed.
ELSE.
LOOP AT i_textdata_tab ASSIGNING <t>.
TRY.
* write data in file
TRANSFER <t> TO i_file.
* conversion erortable does not exist
CATCH cx_sy_codepage_converter_init cx_sy_conversion_codepage.
CLEAR ls_ret.
* MESSAGE S003(EHSBM_LS).
* Fehler bei der Konvertierung der sprachabhängigen Daten
ls_ret-type = 'W'.
ls_ret-id = 'EHSBM_LS'.
ls_ret-number = 003.
APPEND ls_ret TO e_error_tab.
CLEAR ls_ret.
* MESSAGE S137(C$) with I_TEXTDATA_TAB.
ls_ret-type = 'W'.
ls_ret-id = 'C$'.
ls_ret-number = 137.
ls_ret-message_v1 = <t>.
APPEND ls_ret TO e_error_tab.
ENDTRY.
ENDLOOP.
ENDIF.
* close the dataset
CLOSE DATASET i_file.
SET LOCALE LANGUAGE space.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_COPY
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SOURCE TYPE STRING
* | [--->] I_DEST TYPE STRING
* | [EXC!] SOURCE_NOT_EXISTS
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* | [EXC!] CLOSE_FAILED
* | [EXC!] WRITE_FAILED
* | [EXC!] READ_ERROR
* | [EXC!] PATH_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_copy.
DATA l_exist(1).
DATA l_filesize TYPE i.
DATA t_data TYPE solix_tab.
l_exist = zcl_gui_backend_services=>file_exist( i_source ).
IF l_exist IS INITIAL.
RAISE source_not_exists.
ENDIF.
CALL METHOD zcl_gui_backend_services=>file_read_bin
EXPORTING
file = i_source
IMPORTING
filesize = l_filesize
RECEIVING
data = t_data
EXCEPTIONS
no_permission = 1
open_failed = 2
read_error = 3
path_error = 4
OTHERS = 5.
CASE sy-subrc.
WHEN 0.
WHEN 1. RAISE no_permission.
WHEN 2. RAISE open_failed.
WHEN 3. RAISE read_error.
WHEN 4. RAISE path_error.
WHEN OTHERS.
EXIT.
ENDCASE.
CALL METHOD zcl_gui_backend_services=>file_write_bin
EXPORTING
data = t_data
file = i_dest
i_file_overwrite = 'X'
i_file_size = l_filesize
EXCEPTIONS
no_permission = 1
open_failed = 2
ap_file_exists = 3
close_failed = 4
write_failed = 5
OTHERS = 6.
CASE sy-subrc.
WHEN 0.
WHEN 1. RAISE no_permission.
WHEN 2. RAISE open_failed.
WHEN 3. RAISE ap_file_exists.
WHEN 4. RAISE close_failed .
WHEN 5. RAISE write_failed.
WHEN OTHERS.
EXIT.
ENDCASE.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_DELETE
* +-------------------------------------------------------------------------------------------------+
* | [--->] FILE TYPE STRING
* | [EXC!] NO_PERMISSION
* | [EXC!] FAILURE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_delete.
DATA rc TYPE sy-subrc.
"DATA i_file TYPE rcgiedial-iefile.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'DELETE' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
"i_file = file.
DELETE DATASET file.
IF rc <> 0.
RAISE failure.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_EXIST
* +-------------------------------------------------------------------------------------------------+
* | [--->] FILE TYPE STRING
* | [<-()] RESULT TYPE ABAP_BOOL
* | [EXC!] WRONG_PARAMETER
* | [EXC!] NO_AUTHORITY
* | [EXC!] SOURCE_NOT_EXISTS
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_exist.
DATA rc TYPE i.
DATA l_file TYPE rlgrap-filename.
DATA l_dir TYPE EPSF-EPSFILNAM.
DATA l_filemask TYPE EPS2FILNAM.
DATA:it_dirlist TYPE STANDARD TABLE OF eps2fili.
DATA long_filename TYPE dbmsgora-filename.
DATA pure_filename TYPE sdbah-actid.
DATA pure_extension TYPE sdbad-funct.
DATA full_file TYPE dbmsgora-filename.
* check parameter, wild characters not allowed
IF file IS INITIAL OR file CA '*<>|"'.
RAISE wrong_parameter.
EXIT.
ENDIF.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'READ' ).
IF rc <> 0.
RAISE no_authority.
ENDIF.
open dataset file for input
in text mode encoding default
ignoring conversion errors.
IF sy-subrc = 0.
close dataset file.
result = abap_true.
ELSE.
result = abap_false.
ENDIF.
* long_filename = file.
* CALL FUNCTION 'SPLIT_FILENAME'
* EXPORTING
* long_filename = long_filename
* IMPORTING
* pure_filename = pure_filename
* pure_extension = pure_extension.
*
* CONCATENATE pure_filename '.' pure_extension INTO full_file.
*
* l_dir = file.
* REPLACE ALL OCCURRENCES OF full_file IN l_dir WITH ''.
* l_filemask = full_file.
*
* DATA l_dir_s TYPE string.
* DATA l_filemask_s TYPE string.
* DATA l_ext TYPE string.
*
* CALL METHOD zcl_gui_backend_services=>split_filename
* EXPORTING
* file = file
* IMPORTING
* path = l_dir_s
* file_with_ext = l_filemask_s
* ext = l_ext.
*
* l_dir = l_dir_s.
* l_filemask = l_filemask_s.
*
** CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
** EXPORTING
** dir_name = l_dir
** file_mask = l_filemask
** TABLES
** dir_list = it_dirlist[]
** EXCEPTIONS
** invalid_eps_subdir = 1
** sapgparam_failed = 2
** build_directory_failed = 3
** no_authorization = 4
** read_directory_failed = 5
** too_many_read_errors = 6
** empty_directory_list = 7
** OTHERS = 8.
** IF sy-subrc <> 0.
** result = abap_false.
** EXIT.
** ENDIF.
*
*CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING'
* EXPORTING
* iv_dir_name = l_dir
** FILE_MASK = l_filemask
* tables
* dir_list = it_dirlist[]
* EXCEPTIONS
* INVALID_EPS_SUBDIR = 1
* SAPGPARAM_FAILED = 2
* BUILD_DIRECTORY_FAILED = 3
* NO_AUTHORIZATION = 4
* READ_DIRECTORY_FAILED = 5
* TOO_MANY_READ_ERRORS = 6
* EMPTY_DIRECTORY_LIST = 7
* OTHERS = 8.
* .
* IF it_dirlist[] IS INITIAL.
* result = abap_false.
* EXIT.
* ENDIF.
*
* READ TABLE it_dirlist TRANSPORTING NO FIELDS WITH KEY name = l_filemask.
* IF sy-subrc = 0.
* result = abap_true.
* ELSE.
* result = abap_false.
* ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_GET_SIZE
* +-------------------------------------------------------------------------------------------------+
* | [--->] FILE TYPE STRING
* | [<-()] FILE_SIZE TYPE INT4
* | [EXC!] WRONG_PARAMETER
* | [EXC!] NO_AUTHORITY
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_get_size.
DATA rc TYPE i.
DATA l_file TYPE rlgrap-filename.
DATA l_dir TYPE epsf-epsdirnam.
DATA l_filemask TYPE epsf-epsfilnam.
DATA:it_dirlist TYPE STANDARD TABLE OF epsfili.
DATA:ls_dirlist TYPE epsfili.
DATA long_filename TYPE dbmsgora-filename.
DATA pure_filename TYPE sdbah-actid.
DATA pure_extension TYPE sdbad-funct.
DATA full_file TYPE dbmsgora-filename.
* check parameter, wild characters not allowed
IF file IS INITIAL OR file CA '*<>|"'.
RAISE wrong_parameter.
EXIT.
ENDIF.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'READ' ).
IF rc <> 0.
RAISE no_authority.
ENDIF.
DATA l_dir_s TYPE string.
DATA l_filemask_s TYPE string.
DATA l_ext TYPE string.
CALL METHOD zcl_gui_backend_services=>split_filename
EXPORTING
file = file
IMPORTING
path = l_dir_s
file_with_ext = l_filemask_s
ext = l_ext.
l_dir = l_dir_s.
l_filemask = l_filemask_s.
CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
EXPORTING
dir_name = l_dir
file_mask = l_filemask
TABLES
dir_list = it_dirlist[]
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
IF sy-subrc <> 0.
EXIT.
ENDIF.
READ TABLE it_dirlist INTO ls_dirlist WITH KEY name = l_filemask.
IF sy-subrc = 0.
file_size = ls_dirlist-size.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_MOVE
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SOURCE TYPE STRING
* | [--->] I_DEST TYPE STRING
* | [EXC!] SOURCE_NOT_EXISTS
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* | [EXC!] CLOSE_FAILED
* | [EXC!] WRITE_FAILED
* | [EXC!] READ_ERROR
* | [EXC!] PATH_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_move.
DATA l_exist(1).
DATA l_filesize TYPE i.
DATA t_data TYPE solix_tab.
DATA l_ok(1).
l_exist = zcl_gui_backend_services=>file_exist( i_source ).
IF l_exist IS INITIAL.
RAISE source_not_exists.
ENDIF.
CALL METHOD zcl_gui_backend_services=>file_read_bin
EXPORTING
file = i_source
IMPORTING
filesize = l_filesize
RECEIVING
data = t_data
EXCEPTIONS
no_permission = 1
open_failed = 2
read_error = 3
path_error = 4
OTHERS = 5.
CASE sy-subrc.
WHEN 1. RAISE no_permission.
WHEN 2. RAISE open_failed.
WHEN 3. RAISE read_error.
WHEN 4. RAISE path_error.
WHEN OTHERS.
EXIT.
ENDCASE.
CALL METHOD zcl_gui_backend_services=>file_write_bin
EXPORTING
data = t_data
file = i_dest
i_file_overwrite = 'X'
i_file_size = l_filesize
EXCEPTIONS
no_permission = 1
open_failed = 2
ap_file_exists = 3
close_failed = 4
write_failed = 5
OTHERS = 6.
CASE sy-subrc.
WHEN 1. RAISE no_permission.
WHEN 2. RAISE open_failed.
WHEN 3. RAISE ap_file_exists.
WHEN 4. RAISE close_failed .
WHEN 5. RAISE write_failed.
WHEN OTHERS.
EXIT.
ENDCASE.
CALL METHOD zcl_gui_backend_services=>file_delete
EXPORTING
file = i_source
EXCEPTIONS
no_permission = 1
OTHERS = 2.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_READ_BIN
* +-------------------------------------------------------------------------------------------------+
* | [--->] FILE TYPE STRING
* | [<---] FILESIZE TYPE I
* | [<-()] DATA TYPE SOLIX_TAB
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] READ_ERROR
* | [EXC!] PATH_ERROR
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_read_bin.
* lokal data -----------------------------------------------------------
DATA l_len LIKE sy-tabix.
DATA l_subrc LIKE sy-subrc.
" DATA i_file TYPE rcgiedial-iefile.
DATA i_file(200) .
DATA rc TYPE sy-subrc.
DATA ls_data TYPE solix.
* DATA e_file_size TYPE drao-orln.
DATA e_file_size(12) TYPE n.
DATA e_lines TYPE i.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'READ' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
REFRESH data.
** read the raw-file from the appl.server
CLEAR l_subrc.
CATCH SYSTEM-EXCEPTIONS open_dataset_no_authority = 1
dataset_too_many_files = 2
OTHERS = 4.
i_file = file.
OPEN DATASET i_file FOR INPUT IN BINARY MODE.
l_subrc = sy-subrc.
ENDCATCH.
IF sy-subrc <> 0 OR
l_subrc <> 0.
RAISE open_failed.
ENDIF.
CATCH SYSTEM-EXCEPTIONS dataset_read_error = 11
OTHERS = 12.
DO.
CLEAR l_len.
CLEAR ls_data.
READ DATASET i_file INTO ls_data LENGTH l_len.
IF sy-subrc <> 0.
IF l_len > 0.
e_file_size = e_file_size + l_len.
APPEND ls_data TO data.
ENDIF.
EXIT.
ENDIF.
e_file_size = e_file_size + l_len.
APPEND ls_data TO data.
ENDDO.
ENDCATCH.
IF sy-subrc > 10.
RAISE read_error.
ENDIF.
DESCRIBE TABLE data LINES e_lines.
filesize = e_file_size.
CATCH SYSTEM-EXCEPTIONS dataset_cant_close = 1
OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
* but there wasn't any error at the reading of the data
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_READ_TEXT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_FILE TYPE STRING
* | [--->] I_SEP TYPE CHAR01(optional)
* | [<-->] I_TEXTDATA_TAB TYPE STANDARD TABLE
* | [EXC!] ERROR_OPENING_FILE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_read_text.
DATA l_line TYPE string.
DATA t_split TYPE tab512.
DATA lt TYPE TABLE OF tab512.
DATA ls TYPE tab512.
DATA l_idx TYPE i.
FIELD-SYMBOLS:
<ls> TYPE tab512,
<dyn_rec> TYPE any,
<dyn_field> TYPE any.
*------------------------------------------------------------------------------*
* read the file from the application server
*------------------------------------------------------------------------------*
REFRESH i_textdata_tab.
OPEN DATASET i_file WITH WINDOWS LINEFEED FOR INPUT IN TEXT MODE ENCODING NON-UNICODE.
IF sy-subrc NE 0.
RAISE error_opening_file.
ELSE.
WHILE ( sy-subrc EQ 0 ).
READ DATASET i_file INTO l_line.
IF NOT l_line IS INITIAL.
IF i_sep IS INITIAL.
APPEND l_line TO i_textdata_tab.
ELSE.
SPLIT l_line AT i_sep INTO TABLE lt.
APPEND INITIAL LINE TO i_textdata_tab.
READ TABLE i_textdata_tab ASSIGNING <dyn_rec> INDEX sy-tabix.
LOOP AT lt INTO ls.
l_idx = sy-tabix.
ASSIGN COMPONENT l_idx OF STRUCTURE <dyn_rec> TO <dyn_field>.
<dyn_field> = ls-wa.
ENDLOOP.
ENDIF.
ENDIF.
CLEAR l_line.
ENDWHILE.
ENDIF.
CLOSE DATASET i_file.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_WRITE_BIN
* +-------------------------------------------------------------------------------------------------+
* | [--->] DATA TYPE SOLIX_TAB
* | [--->] FILE TYPE STRING
* | [--->] I_FILE_OVERWRITE TYPE ABAP_BOOL (default =' ')
* | [--->] I_FILE_SIZE TYPE I (default =0)
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* | [EXC!] CLOSE_FAILED
* | [EXC!] WRITE_FAILED
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_write_bin.
DATA l_len TYPE i.
DATA l_all_lines_len TYPE i.
DATA l_diff_len TYPE i.
DATA l_subrc LIKE sy-subrc.
DATA rc TYPE sy-subrc.
DATA ls_data TYPE solix.
DATA e_file_size(12) TYPE n.
" DATA e_file_size TYPE drao-orln.
DATA i_lines TYPE i.
" DATA i_file TYPE rcgiedial-iefile.
DATA i_file TYPE char200.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'WRITE' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'DELETE' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
i_file = file.
* check if file exists if we arn't allowed to overwrite file
IF i_file_overwrite = ' '. " apap_FALSE'.
CLEAR l_subrc.
CATCH SYSTEM-EXCEPTIONS open_dataset_no_authority = 1
dataset_too_many_files = 2
OTHERS = 4.
OPEN DATASET i_file FOR INPUT IN BINARY MODE.
l_subrc = sy-subrc.
ENDCATCH.
IF l_subrc <> 0.
* nothing
ELSE.
CATCH SYSTEM-EXCEPTIONS OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
RAISE ap_file_exists.
ENDIF.
CATCH SYSTEM-EXCEPTIONS OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
ENDIF.
i_lines = lines( data ).
* open dataset for writing
OPEN DATASET i_file FOR OUTPUT IN BINARY MODE.
IF NOT sy-subrc IS INITIAL.
RAISE open_failed.
ELSE.
l_len = lg_max_len.
LOOP AT data INTO ls_data.
* last line is shorter perhaps
IF sy-tabix = i_lines.
l_all_lines_len = lg_max_len * ( i_lines - 1 ).
l_diff_len = i_file_size - l_all_lines_len.
l_len = l_diff_len.
ENDIF.
* write data in file
CATCH SYSTEM-EXCEPTIONS dataset_write_error = 1
OTHERS = 4.
TRANSFER ls_data TO i_file LENGTH l_len.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
RAISE write_failed.
ENDIF.
ENDLOOP.
ENDIF.
* close the dataset
CATCH SYSTEM-EXCEPTIONS dataset_cant_close = 1
OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
RAISE close_failed.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_WRITE_BIN2
* +-------------------------------------------------------------------------------------------------+
* | [--->] DATA TYPE STANDARD TABLE
* | [--->] FILE TYPE STRING
* | [--->] I_FILE_OVERWRITE TYPE ABAP_BOOL (default =' ')
* | [--->] I_FILE_SIZE TYPE I (default =0)
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* | [EXC!] CLOSE_FAILED
* | [EXC!] WRITE_FAILED
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_write_bin2.
DATA l_len TYPE i.
DATA l_all_lines_len TYPE i.
DATA l_diff_len TYPE i.
DATA l_subrc LIKE sy-subrc.
DATA rc TYPE sy-subrc.
DATA ls_data TYPE solix.
DATA e_file_size(12) TYPE n.
" DATA e_file_size TYPE drao-orln.
DATA i_lines TYPE i.
" DATA i_file TYPE rcgiedial-iefile.
DATA i_file TYPE char200.
DATA lv_max_len TYPE i.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'WRITE' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
rc = zcl_gui_backend_services=>check_authority( i_file = file activity = 'DELETE' ).
IF rc <> 0.
RAISE no_permission.
ENDIF.
i_file = file.
* check if file exists if we arn't allowed to overwrite file
IF i_file_overwrite = ' '. " apap_FALSE'.
CLEAR l_subrc.
CATCH SYSTEM-EXCEPTIONS open_dataset_no_authority = 1
dataset_too_many_files = 2
OTHERS = 4.
OPEN DATASET i_file FOR INPUT IN BINARY MODE.
l_subrc = sy-subrc.
ENDCATCH.
IF l_subrc <> 0.
* nothing
ELSE.
CATCH SYSTEM-EXCEPTIONS OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
RAISE ap_file_exists.
ENDIF.
CATCH SYSTEM-EXCEPTIONS OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
ENDIF.
i_lines = lines( data ).
* open dataset for writing
OPEN DATASET i_file FOR OUTPUT IN BINARY MODE.
IF NOT sy-subrc IS INITIAL.
RAISE open_failed.
ELSE.
l_len = lg_max_len.
DESCRIBE TABLE data.
lv_max_len = sy-tleng.
LOOP AT data INTO ls_data.
"if sy-tabix = 1. lv_maxlen = xstrlen( ls_data ).ENDIF.
* last line is shorter perhaps
IF sy-tabix = i_lines.
l_all_lines_len = lv_max_len * ( i_lines - 1 ).
l_diff_len = i_file_size - l_all_lines_len.
l_len = l_diff_len.
ENDIF.
* write data in file
CATCH SYSTEM-EXCEPTIONS dataset_write_error = 1
OTHERS = 4.
TRANSFER ls_data TO i_file LENGTH l_len.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
RAISE write_failed.
ENDIF.
ENDLOOP.
ENDIF.
* close the dataset
CATCH SYSTEM-EXCEPTIONS dataset_cant_close = 1
OTHERS = 4.
CLOSE DATASET i_file.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
RAISE close_failed.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_WRITE_TEXT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_FILE TYPE STRING(optional)
* | [--->] I_FILE_OVERWRITE TYPE XFELD(optional)
* | [--->] I_LANGU TYPE SY-LANGU (default =SY-LANGU)
* | [<---] E_ERROR_TAB TYPE BAPIRET2_TAB
* | [<-->] I_TEXTDATA_TAB TYPE STANDARD TABLE
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_write_text.
* local data -----------------------------------------------------------
DATA l_filename TYPE authb-filename.
DATA ls_ret TYPE bapiret2.
FIELD-SYMBOLS <t> TYPE any.
* function body --------------------------------------------------------
* init
l_filename = i_file.
* check the authority for file
CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
EXPORTING
activity = sabc_act_write
* Authority Check allows right now only 60 Character
filename = l_filename(60)
EXCEPTIONS
no_authority = 1
activity_unknown = 2
OTHERS = 3.
IF sy-subrc <> 0.
RAISE no_permission.
ENDIF.
* check if file exists if we arn't allowed to overwrite file
IF i_file_overwrite IS INITIAL.
OPEN DATASET i_file FOR INPUT IN TEXT MODE
ENCODING NON-UNICODE.
IF sy-subrc IS INITIAL.
CLOSE DATASET i_file.
RAISE ap_file_exists.
ENDIF.
CLOSE DATASET i_file.
ENDIF.
SET LOCALE LANGUAGE i_langu.
* open dataset for writing
OPEN DATASET i_file WITH WINDOWS LINEFEED FOR OUTPUT IN TEXT MODE
ENCODING NON-UNICODE.
IF NOT sy-subrc IS INITIAL.
RAISE open_failed.
ELSE.
LOOP AT i_textdata_tab ASSIGNING <t>.
TRY.
* write data in file
TRANSFER <t> TO i_file.
* conversion erortable does not exist
CATCH cx_sy_codepage_converter_init cx_sy_conversion_codepage.
CLEAR ls_ret.
* MESSAGE S003(EHSBM_LS).
* Fehler bei der Konvertierung der sprachabhängigen Daten
ls_ret-type = 'W'.
ls_ret-id = 'EHSBM_LS'.
ls_ret-number = 003.
APPEND ls_ret TO e_error_tab.
CLEAR ls_ret.
* MESSAGE S137(C$) with I_TEXTDATA_TAB.
ls_ret-type = 'W'.
ls_ret-id = 'C$'.
ls_ret-number = 137.
ls_ret-message_v1 = <t>.
APPEND ls_ret TO e_error_tab.
ENDTRY.
ENDLOOP.
ENDIF.
* close the dataset
CLOSE DATASET i_file.
SET LOCALE LANGUAGE space.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>FILE_WRITE_TEXT_TEST
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_FILE TYPE STRING(optional)
* | [--->] I_FILE_OVERWRITE TYPE XFELD(optional)
* | [--->] I_LANGU TYPE SY-LANGU (default =SY-LANGU)
* | [<---] E_ERROR_TAB TYPE BAPIRET2_TAB
* | [<-->] I_TEXTDATA_TAB TYPE STANDARD TABLE
* | [EXC!] NO_PERMISSION
* | [EXC!] OPEN_FAILED
* | [EXC!] AP_FILE_EXISTS
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD file_write_text_test.
* local data -----------------------------------------------------------
DATA l_filename TYPE authb-filename.
DATA ls_ret TYPE bapiret2.
FIELD-SYMBOLS <t> TYPE any.
DATA ctab TYPE TABLE OF TRUXS_T_TEXT_DATA.
* function body --------------------------------------------------------
* init
l_filename = i_file.
* check the authority for file
CALL FUNCTION 'AUTHORITY_CHECK_DATASET'
EXPORTING
activity = sabc_act_write
* Authority Check allows right now only 60 Character
filename = l_filename(60)
EXCEPTIONS
no_authority = 1
activity_unknown = 2
OTHERS = 3.
IF sy-subrc <> 0.
RAISE no_permission.
ENDIF.
* check if file exists if we arn't allowed to overwrite file
IF i_file_overwrite IS INITIAL.
OPEN DATASET i_file FOR INPUT IN TEXT MODE
ENCODING NON-UNICODE.
IF sy-subrc IS INITIAL.
CLOSE DATASET i_file.
RAISE ap_file_exists.
ENDIF.
CLOSE DATASET i_file.
ENDIF.
SET LOCALE LANGUAGE i_langu.
* open dataset for writing
OPEN DATASET i_file WITH WINDOWS LINEFEED FOR OUTPUT IN TEXT MODE
ENCODING NON-UNICODE.
IF NOT sy-subrc IS INITIAL.
RAISE open_failed.
ELSE.
LOOP AT I_TEXTDATA_TAB ASSIGNING <t>.
TRY.
* write data in file
TRANSFER <t> TO i_file.
* conversion erortable does not exist
CATCH cx_sy_codepage_converter_init cx_sy_conversion_codepage.
CLEAR ls_ret.
* MESSAGE S003(EHSBM_LS).
* Fehler bei der Konvertierung der sprachabhängigen Daten
ls_ret-type = 'W'.
ls_ret-id = 'EHSBM_LS'.
ls_ret-number = 003.
APPEND ls_ret TO e_error_tab.
CLEAR ls_ret.
* MESSAGE S137(C$) with I_TEXTDATA_TAB.
ls_ret-type = 'W'.
ls_ret-id = 'C$'.
ls_ret-number = 137.
ls_ret-message_v1 = <t>.
APPEND ls_ret TO e_error_tab.
ENDTRY.
ENDLOOP.
ENDIF.
* close the dataset
CLOSE DATASET i_file.
SET LOCALE LANGUAGE space.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>GET_FILE_SEPARATOR
* +-------------------------------------------------------------------------------------------------+
* | [<-()] SEP TYPE CHAR01
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_file_separator.
*
** Ermittle Trenner
* IF sy-opsys CS 'WINDOWS'.
** Windows NT/2000
* sep = '\'.
* ELSE.
** UNIX
* sep = '/'.
* ENDIF.
DATA l_value TYPE text255.
CALL FUNCTION 'RSPO_R_SAPGPARAM'
EXPORTING
name = 'DIR_SEP'
IMPORTING
value = l_value
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
sep = l_value.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>MV
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SOURCE TYPE STRING
* | [--->] I_DEST TYPE STRING
* | [--->] I_FILE TYPE STRING
* | [<-()] RC TYPE SY-SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD mv.
DATA cmd TYPE string.
CONCATENATE i_source i_file INTO cmd .
CONCATENATE 'mv' cmd i_dest INTO cmd SEPARATED BY space.
CALL FUNCTION 'Z_BC_RUN_CONSOLE'
EXPORTING
cmd = cmd
IMPORTING
rc = rc.
* TABLES
* RESULT =
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>REN
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SOURCE TYPE STRING
* | [--->] I_DEST TYPE STRING
* | [--->] I_FILE1 TYPE STRING
* | [--->] I_FILE2 TYPE STRING
* | [<-()] RC TYPE SY-SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD ren.
DATA tmp TYPE string.
DATA cmd TYPE string.
CONCATENATE i_source i_file1 INTO tmp .
CONCATENATE i_dest i_file2 INTO cmd.
CONCATENATE 'mv' tmp cmd INTO cmd SEPARATED BY space.
CALL FUNCTION 'Z_BC_RUN_CONSOLE'
EXPORTING
cmd = cmd
IMPORTING
rc = rc.
* TABLES
* RESULT =
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_GUI_BACKEND_SERVICES=>SPLIT_FILENAME
* +-------------------------------------------------------------------------------------------------+
* | [--->] FILE TYPE STRING
* | [<---] PATH TYPE STRING
* | [<---] FILE_WITH_EXT TYPE STRING
* | [<---] EXT TYPE STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD split_filename.
DATA long_filename TYPE dbmsgora-filename.
DATA pure_filename TYPE sdbah-actid.
DATA pure_extension TYPE sdbad-funct.
DATA full_file TYPE dbmsgora-filename.
long_filename = file.
path = file.
CALL FUNCTION 'SPLIT_FILENAME'
EXPORTING
long_filename = long_filename
IMPORTING
pure_filename = pure_filename
pure_extension = pure_extension.
CONCATENATE pure_filename '.' pure_extension INTO full_file.
file_with_ext = full_file.
ext = pure_extension.
REPLACE ALL OCCURRENCES OF full_file IN path WITH ''.
ENDMETHOD.
ENDCLASS.
Code: Alles auswählen.
FUNCTION z_bc_run_console.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(CMD) TYPE STRING
*" EXPORTING
*" REFERENCE(RC) TYPE SY-SUBRC
*" TABLES
*" RESULT STRUCTURE TAB512 OPTIONAL
*"----------------------------------------------------------------------
DATA:tabl TYPE TABLE OF tab512 WITH HEADER LINE.
DATA tpcmd(1024).
* eg: mv /home/jack/testfile.txt /home/jack/Documents/
tpcmd = cmd.
REFRESH tabl.
CALL 'SYSTEM' ID 'COMMAND' FIELD tpcmd ID 'TAB' FIELD tabl-*sys*.
result[] = tabl[].
rc = sy-subrc.
ENDFUNCTION.
Code: Alles auswählen.
Checks if a File Exists
get file size
Checks Authority for file operations on Backend
determins file / path separator
Read file from Backend into SOLIX_tab
Write file from SOLIX_tab into Backend
Write file from any tab to Backend
Delete file
list files in directory
list files in directory via system cmd (unix)
list files in directory
Split Filename from Path
Append text to file on Application Server
Create text file on Application Server
Create text file on Application Server
Read Path from Alias (e.g. DIR_TRANS)
copy file
copy file
move file
rename file
move file
read windows Textfile
Checks Authority for Sys-Commands
condense text line with separator