AL11 Log Deletion

Aim:
    To delete files in AL11 based on date range.

Program:

REPORT al11_log_deletion.

*==================
* Data Declarations
*==================
TYPE-POOLSicon.
* For Standard Program RSWATCH0 Data
DATAi_outtab  TYPE STANDARD TABLE OF cst_rswatch01_alv,
      wa_outtab TYPE cst_rswatch01_alv,
      g_fname   TYPE char80 VALUE '(RSWATCH0)GT_OUTTAB'.
FIELD-SYMBOLS <fs_outtab> TYPE STANDARD TABLE.

* Structure for Directory Name
TYPESBEGIN OF t_outtab1,
        dirname TYPE shvalue_d,
       END OF t_outtab1.

* Declaration of variables for FM - F4IF_INT_TABLE_VALUE_REQUEST
DATAi_outtab1   TYPE STANDARD TABLE OF t_outtab1,
      wa_outtab1   TYPE t_outtab1,
      i_return LIKE ddshretval OCCURS WITH HEADER LINE.

* Declarations for getting files' info from directory
DATAi_filed TYPE TABLE OF /sdf/file,
      wa_filed TYPE /sdf/file,
      g_dname TYPE epsf-epsdirnam."/sdf/file-name.


* Declarations for the final table that will hold the required files' details for output
TYPESBEGIN OF t_changing,
        name TYPE /sdf/file-name,
        type TYPE /sdf/file-type,
        size TYPE /sdf/file-len,
        date TYPE /sdf/file-mod_date,
        time TYPE /sdf/file-mod_time,
       END OF t_changing.

* Final Internal Table
DATA:  i_ft TYPE TABLE OF t_changing,
       wa_ft TYPE t_changing.
 
DATAl_pos TYPE i,                             " Used to find the file name
      l_strln TYPE i,                           " String length of l_rev
      l_rev TYPE cst_rswatch01_alv-dirname,     " Reversed string which has directory and file
      l_file1 TYPE cst_rswatch01_alv-dirname,   " Reversed file name
      l_file2 TYPE cst_rswatch01_alv-dirname,   " File name
      l_dir TYPE epsf-epsdirnam,                " Reversed directory
      l_fdir TYPE epsf-epsdirnam.               " Directory

* Deletion final path
DATAl_path TYPE cst_rswatch01_alv-dirname.
DATAl_answer(1TYPE c,
      l_records TYPE string,
      l_text TYPE string.

* Inputs
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
  PARAMETERSp_dir TYPE epsf-epsdirnam LOWER CASE DEFAULT '/data' OBLIGATORY.
  SELECT-OPTIONSs_date FOR sy-datum OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

*======================
* Initialization Event
*======================
INITIALIZATION.

* Fetch Directory Names from RSWATCH0
  PERFORM main IN PROGRAM rswatch0.

  ASSIGN (g_fnameTO <fs_outtab>.

  CHECK sy-subrc EQ 0.

  i_outtab <fs_outtab>.

* Move the fetched directory names from I_OUTTAB to I_OUTTAB1 which supports Search Help logic
  LOOP AT i_outtab INTO wa_outtab.
    IF wa_outtab-dirname+0(5'/data'.
      wa_outtab1-dirname wa_outtab-dirname.
      APPEND wa_outtab1 TO i_outtab1.
      CLEARwa_outtab1wa_outtab.
    ENDIF.
  ENDLOOP.

*=============================
* At Selection-screen event
*=============================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dir.
  CLEARi_return.
* Function Module for F4 Help
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        'DIRNAME'
      dynpprog        sy-cprog
      dynpnr          sy-dynnr
      dynprofield     'P_DIR'
      value_org       'S'
    TABLES
      value_tab       i_outtab1
      return_tab      i_return
    EXCEPTIONS
      parameter_error 1
      no_values_found 2
      OTHERS          3.

  IF sy-subrc 0.
    p_dir i_return-fieldval.
  ENDIF.

AT SELECTION-SCREEN ON p_dir.
  IF p_dir IS NOT INITIAL.
    IF p_dir+0(5<> '/data'.
      MESSAGE text-102 TYPE 'E'.
    ENDIF.
  ENDIF.

  READ TABLE i_outtab1 WITH KEY dirname p_dir TRANSPORTING NO FIELDS.
  IF sy-subrc <> 0.

* Reversing the input (p_dir)
    CALL FUNCTION 'STRING_REVERSE'
      EXPORTING
        string    p_dir
        lang      sy-langu
      IMPORTING
        rstring   l_rev
      EXCEPTIONS
        too_small 1
        OTHERS    2.

    IF sy-subrc 0.
      FIND FIRST OCCURRENCE OF '/' IN l_rev MATCH OFFSET l_pos.
      IF l_pos <> 0.
        l_file1 l_rev+0(l_pos).
        l_strln strlenl_rev ).
        l_dir l_rev+l_pos(l_strln).

* To get the file name
        CALL FUNCTION 'STRING_REVERSE'
          EXPORTING
            string    l_file1
            lang      sy-langu
          IMPORTING
            rstring   l_file2
          EXCEPTIONS
            too_small 1
            OTHERS    2.
        IF sy-subrc <> 0.
          MESSAGE text-103 TYPE 'E'.
        ENDIF.

* To get the directory
        CALL FUNCTION 'STRING_REVERSE'
          EXPORTING
            string    l_dir
            lang      sy-langu
          IMPORTING
            rstring   l_fdir
          EXCEPTIONS
            too_small 1
            OTHERS    2.

        IF sy-subrc 0.
          g_dname l_fdir.
        ENDIF.
      ELSE.
        MESSAGE text-102 TYPE 'E'.
      ENDIF.
    ENDIF.
  ELSE.
    g_dname p_dir.
  ENDIF.

*=============================
* Start of Selection event
*=============================
START-OF-SELECTION.

  REFRESHi_fti_filed.

* Function Module to get the files from the specified directory
  PERFORM get_file_attributes.

* Filtering the files based on given date range
  IF l_file2 IS NOT INITIAL.
    LOOP AT i_filed INTO wa_filed WHERE mod_date IN s_date AND
                                        type 'file, regu'.

      IF wa_filed-name CP l_file2.  " Contains Pattern Check
        wa_ft-name wa_filed-name.
        wa_ft-type wa_filed-type.
        wa_ft-size wa_filed-len.
        wa_ft-date wa_filed-mod_date.
        wa_ft-time wa_filed-mod_time.
        APPEND wa_ft TO i_ft.
        CLEARwa_ftwa_filed.
      ENDIF.
    ENDLOOP.
  ELSE.

    LOOP AT i_filed INTO wa_filed WHERE  mod_date IN s_date AND
                                         type 'file, regu'.
      wa_ft-name wa_filed-name.
      wa_ft-type wa_filed-type.
      wa_ft-size wa_filed-len.
      wa_ft-date wa_filed-mod_date.
      wa_ft-time wa_filed-mod_time.
      APPEND wa_ft TO i_ft.
      CLEARwa_ftwa_filed.
    ENDLOOP.
  ENDIF.

* Deletion logic
  IF i_ft IS NOT INITIAL.
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar              text-106
        text_question         text-107
        text_button_1         'Yes'
        icon_button_1         '@B4@'
        text_button_2         'No'
        icon_button_2         '@3U@'
        display_cancel_button ''
        default_button        '2'
      IMPORTING
        answer                l_answer
      EXCEPTIONS
        text_not_found        1
        OTHERS                2.
    IF sy-subrc EQ 0.
      IF l_answer '1'.
        DESCRIBE TABLE i_ft LINES l_records.
        LOOP AT i_ft INTO wa_ft.
          CONCATENATE g_dname '/' wa_ft-name INTO l_path.
          DELETE DATASET l_path.
          CLEARl_path.
        ENDLOOP.
        IF sy-subrc 0.
          l_text text-101.
          REPLACE '&1' WITH l_records INTO l_text.
          MESSAGE l_text TYPE 'S'.
        ENDIF.
      ELSEIF l_answer '2'.
        MESSAGE text-105 TYPE 'I'.
      ENDIF.
    ENDIF.
  ELSE.
    MESSAGE text-104 TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  GET_FILE_ATTRIBUTES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file_attributes .

  TABLES epsf.
  DATAi_file_list TYPE TABLE OF epsfili,
        wa_file_list TYPE epsfili.
  DATA l_mtime TYPE DECIMALS 0.
  DATA l_time(10).
  DATA l_date LIKE sy-datum.

  CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
    EXPORTING
      dir_name               g_dname
    TABLES
      dir_list               i_file_list
    EXCEPTIONS
      invalid_eps_subdir     11
      sapgparam_failed       12
      build_directory_failed 13
      no_authorization       14
      read_directory_failed  17
      too_many_read_errors   22
      empty_directory_list   23.

  IF sy-subrc NE 0.
    MESSAGE text-104 TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  IF i_file_list IS NOT INITIAL.
    LOOP AT i_file_list INTO wa_file_list.
      CALL FUNCTION 'EPS_GET_FILE_ATTRIBUTES'
        EXPORTING
          file_name              wa_file_list-name
          dir_name               g_dname
        IMPORTING
          file_size              epsf-epsfilsiz
          file_owner             epsf-epsfilown
          file_mode              epsf-epsfilmod
          file_type              epsf-epsfiltyp
          file_mtime             l_mtime
        EXCEPTIONS
          read_directory_failed  1
          read_attributes_failed 2
          OTHERS                 3.

      IF sy-subrc EQ 0.
        PERFORM p6_to_date_time_tz(rstr0400USING l_mtime
                                                   l_time
                                                   l_date.
        wa_filed-name wa_file_list-name.
        wa_filed-type epsf-epsfiltyp.
        wa_filed-len epsf-epsfilsiz.
        wa_filed-mtime l_mtime.
        wa_filed-mod_date l_date.
        wa_filed-mod_time l_time.
        APPEND wa_filed TO i_filed.
        CLEAR wa_filed.
      ENDIF.
    ENDLOOP.
  ENDIF.
ENDFORM.                    " GET_FILE_ATTRIBUTES

Text Elements:
101    &1file(s) deleted successfully
102    Enter a valid directory name starting with ''/data''
103    File name reversal not successful
104    No file found
105    Deletion Cancelled
106    Files' Deletion Confirmation
107    Are you sure you want to delete the file(s)?


Selection Texts:
P_DIR    Path
S_DATE    Date Range 

Comments

Popular posts from this blog

EWM PPF Trigger the existing PPF Action through Custom Program

ABAP 7.5 - FILTER Statement

Useful Transaction Codes / Tools in OData ABAP Service / SAP UI5/ SAP Fiori