REPORT ztestr_file_handling.
PARAMETERS : p_path TYPE dxfields-longpath,
p_down TYPE dxfields-longpath.
TYPES : BEGIN OF ty_final,
field1(4) TYPE c,
field2(4) TYPE n,
END OF ty_final.
DATA : git_string TYPE STANDARD TABLE OF string INITIAL SIZE 1,
git_final TYPE STANDARD TABLE OF ty_final INITIAL SIZE 1.
*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'A'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_path
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_down.
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'A'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_down
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
START-OF-SELECTION.
IF p_path IS NOT INITIAL.
*-- Validate whether path exists or not
PERFORM validate_path.
*-- Upload the file
PERFORM upload_file.
IF git_final[] IS NOT INITIAL.
*-- Process the file
PERFORM process_file.
IF p_down IS NOT INITIAL.
*-- Validate whether path exists or not
PERFORM validate_path_d.
*-- Download the file
PERFORM download_file.
ENDIF.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM validate_path .
DATA: lv_file TYPE tpfht-pffile,
lv_true TYPE char1.
CLEAR lv_file.
lv_file = p_path.
CALL FUNCTION 'PFL_CHECK_OS_FILE_EXISTENCE'
EXPORTING
fully_qualified_filename = lv_file
IMPORTING
file_exists = lv_true.
IF lv_true = space.
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " VALIDATE_PATH
*&---------------------------------------------------------------------*
*& Form UPLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload_file .
DATA : lv_filename TYPE string,
lwa_data TYPE string,
lwa_final TYPE ty_final.
lv_filename = p_path.
*-- Read data from Application Server
OPEN DATASET lv_filename FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc IS INITIAL.
DO.
READ DATASET lv_filename INTO lwa_data.
IF sy-subrc EQ 0.
SPLIT lwa_data AT '|' INTO lwa_final-field1 lwa_final-field2.
APPEND lwa_final TO git_final.
CLEAR: lwa_final.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET lv_filename.
ELSE.
MESSAGE 'Path does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " UPLOAD_FILE
*&---------------------------------------------------------------------*
*& Form PROCESS_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_file .
FIELD-SYMBOLS : <fwa_final> TYPE ty_final.
LOOP AT git_final ASSIGNING <fwa_final>.
<fwa_final>-field2 = <fwa_final>-field2 + 1.
CONDENSE <fwa_final>-field2.
ENDLOOP.
ENDFORM. " PROCESS_FILE
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_file .
DATA : lv_filename TYPE string,
lwa_string TYPE string,
lit_string TYPE STANDARD TABLE OF string INITIAL SIZE 1.
FIELD-SYMBOLS : <fwa_final> TYPE ty_final.
IF p_down IS NOT INITIAL.
lv_filename = p_down.
IF git_final[] IS NOT INITIAL.
LOOP AT git_final ASSIGNING <fwa_final>.
CONCATENATE <fwa_final>-field1 <fwa_final>-field2 INTO lwa_string
SEPARATED BY '|'.
APPEND lwa_string TO lit_string.
CLEAR lwa_string.
ENDLOOP.
ENDIF.
* // Upload error file to Application Layer
OPEN DATASET lv_filename FOR OUTPUT IN BINARY MODE.
IF sy-subrc IS INITIAL.
LOOP AT lit_string INTO lwa_string.
TRANSFER lwa_string TO lv_filename.
ENDLOOP.
CLOSE DATASET lv_filename.
ENDIF.
ENDIF.
ENDFORM. " DOWNLOAD_FILE
*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH_D
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM validate_path_d .
DATA: lv_file TYPE tpfht-pffile,
lv_true TYPE char1.
CLEAR lv_file.
lv_file = p_down.
CALL FUNCTION 'PFL_CHECK_OS_FILE_EXISTENCE'
EXPORTING
fully_qualified_filename = lv_file
IMPORTING
file_exists = lv_true.
IF lv_true = space.
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " VALIDATE_PATH_D
INPUT file contents
ABCD|1000
EFGH|2000
IJKL|3000
MNOP|4000
QRST|5000
OUTPUT FILE CONTENT :
ABCD|1001
EFGH|2001
IJKL|3001
MNOP|4001
QRST|5001
PARAMETERS : p_path TYPE dxfields-longpath,
p_down TYPE dxfields-longpath.
TYPES : BEGIN OF ty_final,
field1(4) TYPE c,
field2(4) TYPE n,
END OF ty_final.
DATA : git_string TYPE STANDARD TABLE OF string INITIAL SIZE 1,
git_final TYPE STANDARD TABLE OF ty_final INITIAL SIZE 1.
*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'A'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_path
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*-- F4 help for the presentation layer
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_down.
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
i_location_flag = 'A'
* i_server = space
* i_path = lv_i_path
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
* O_LOCATION_FLAG =
* O_SERVER =
o_path = p_down
* ABEND_FLAG =
EXCEPTIONS
rfc_error = 1
error_with_gui = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
START-OF-SELECTION.
IF p_path IS NOT INITIAL.
*-- Validate whether path exists or not
PERFORM validate_path.
*-- Upload the file
PERFORM upload_file.
IF git_final[] IS NOT INITIAL.
*-- Process the file
PERFORM process_file.
IF p_down IS NOT INITIAL.
*-- Validate whether path exists or not
PERFORM validate_path_d.
*-- Download the file
PERFORM download_file.
ENDIF.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM validate_path .
DATA: lv_file TYPE tpfht-pffile,
lv_true TYPE char1.
CLEAR lv_file.
lv_file = p_path.
CALL FUNCTION 'PFL_CHECK_OS_FILE_EXISTENCE'
EXPORTING
fully_qualified_filename = lv_file
IMPORTING
file_exists = lv_true.
IF lv_true = space.
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " VALIDATE_PATH
*&---------------------------------------------------------------------*
*& Form UPLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload_file .
DATA : lv_filename TYPE string,
lwa_data TYPE string,
lwa_final TYPE ty_final.
lv_filename = p_path.
*-- Read data from Application Server
OPEN DATASET lv_filename FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc IS INITIAL.
DO.
READ DATASET lv_filename INTO lwa_data.
IF sy-subrc EQ 0.
SPLIT lwa_data AT '|' INTO lwa_final-field1 lwa_final-field2.
APPEND lwa_final TO git_final.
CLEAR: lwa_final.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET lv_filename.
ELSE.
MESSAGE 'Path does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " UPLOAD_FILE
*&---------------------------------------------------------------------*
*& Form PROCESS_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_file .
FIELD-SYMBOLS : <fwa_final> TYPE ty_final.
LOOP AT git_final ASSIGNING <fwa_final>.
<fwa_final>-field2 = <fwa_final>-field2 + 1.
CONDENSE <fwa_final>-field2.
ENDLOOP.
ENDFORM. " PROCESS_FILE
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_file .
DATA : lv_filename TYPE string,
lwa_string TYPE string,
lit_string TYPE STANDARD TABLE OF string INITIAL SIZE 1.
FIELD-SYMBOLS : <fwa_final> TYPE ty_final.
IF p_down IS NOT INITIAL.
lv_filename = p_down.
IF git_final[] IS NOT INITIAL.
LOOP AT git_final ASSIGNING <fwa_final>.
CONCATENATE <fwa_final>-field1 <fwa_final>-field2 INTO lwa_string
SEPARATED BY '|'.
APPEND lwa_string TO lit_string.
CLEAR lwa_string.
ENDLOOP.
ENDIF.
* // Upload error file to Application Layer
OPEN DATASET lv_filename FOR OUTPUT IN BINARY MODE.
IF sy-subrc IS INITIAL.
LOOP AT lit_string INTO lwa_string.
TRANSFER lwa_string TO lv_filename.
ENDLOOP.
CLOSE DATASET lv_filename.
ENDIF.
ENDIF.
ENDFORM. " DOWNLOAD_FILE
*&---------------------------------------------------------------------*
*& Form VALIDATE_PATH_D
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM validate_path_d .
DATA: lv_file TYPE tpfht-pffile,
lv_true TYPE char1.
CLEAR lv_file.
lv_file = p_down.
CALL FUNCTION 'PFL_CHECK_OS_FILE_EXISTENCE'
EXPORTING
fully_qualified_filename = lv_file
IMPORTING
file_exists = lv_true.
IF lv_true = space.
MESSAGE 'File does not exist' TYPE 'I'.
EXIT.
ENDIF.
ENDFORM. " VALIDATE_PATH_D
INPUT file contents
ABCD|1000
EFGH|2000
IJKL|3000
MNOP|4000
QRST|5000
OUTPUT FILE CONTENT :
ABCD|1001
EFGH|2001
IJKL|3001
MNOP|4001
QRST|5001
It was so nice article and useful to SAP learners. we also provide all SAP Courses online training our Cubtraining is leader in providing Software Training
ReplyDeleteSap Abap : Uploading And Ing File From Application Server In Sap >>>>> Download Now
ReplyDelete>>>>> Download Full
Sap Abap : Uploading And Ing File From Application Server In Sap >>>>> Download LINK
>>>>> Download Now
Sap Abap : Uploading And Ing File From Application Server In Sap >>>>> Download Full
>>>>> Download LINK 9J