Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

File, Directory Operations using Class : CL_GUI_FRONTEND_SERVICES

 

Summary

This Post include the sample program using the class CL_GUI_FRONTEND_SERVICES for carrying out basic File & Directory Operations.

Author(s):  Vinod Kumar T

   
Company:     NIIT Technologies Limited
Created on:    28.07.2010
Author(s) Bio
Vinod Kumar is an ABAP Consultant working with NIIT Technologies Limited.

Sample Program using class CL_GUI_FRONTEND_SERVICES for File,Directory Operations

*&---------------------------------------------------------------------*
*& Report  ZVK_CL_GUI
*&
*&---------------------------------------------------------------------*
*&Sample program for File,Directory Operations using CL_GUI_FRONTEND_SERVICES
*&---------------------------------------------------------------------*

** Text Symbols **

* 001  -  File Operations
* 002  -  ----------------------------------------
* 003  - Directory Operations

** Selection Texts **
* P_DCREAT  - Create Directory
* P_DDEL    - Delete Directory
* P_DEXIST  - Check Existence of Directory
* P_DFLIST  - Directory List
* P_DIR     - Directory Name
* P_DSEL    - Directory Select
* P_FATTR   - Get File Attributes
* P_FCOPY   - Copy File
* P_FDEL    - Delete File
* P_FEXIST  - Check Existence of File
* P_FSEL    - File Select

REPORT  zvk_cl_gui.
TYPE-POOLS : abap.

DATA : it_file_table   TYPE filetable,
       is_file_table   TYPE file_table,
       rc              TYPE i,
       lv_file         TYPE string,
       lv_file_size    TYPE i,
       lv_window_title TYPE string,
       lv_user_action  TYPE i,
       lv_file_count   TYPE i.

DATA : lv_result       TYPE abap_bool,
       lv_multi        TYPE abap_bool,
       lv_readonly     TYPE abap_bool,
       lv_normal       TYPE abap_bool,
       lv_hidden       TYPE abap_bool,
       lv_archive      TYPE abap_bool.

DATA : lv_file_from    TYPE string,
       lv_file_to      TYPE string,
       lv_path         TYPE string,
       lv_fullpath     TYPE string,
       lv_sel_folder   TYPE string.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.  "File Operations"
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 3(30) text-001.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN ULINE 3(50).
PARAMETERS : p_fsel   RADIOBUTTON GROUP g1
                      DEFAULT 'X' USER-COMMAND radio1.     "File Selection"
PARAMETERS : p_fexist RADIOBUTTON GROUP g1.                "Check File Existance"
PARAMETERS : p_fattr  RADIOBUTTON GROUP g1.                "Get File Attributes"
PARAMETERS : p_fcopy  RADIOBUTTON GROUP g1.                "File Copy"
PARAMETERS : p_fdel   RADIOBUTTON GROUP g1.                "Delete Files"

SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 3(30) text-003.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN ULINE 3(50).
PARAMETERS : p_dsel     RADIOBUTTON GROUP g1.                "Directory Select"
PARAMETERS : p_dexist   RADIOBUTTON GROUP g1.                "Dir existence check"
PARAMETERS : p_dflist   RADIOBUTTON GROUP g1.                "Directory List"
PARAMETERS : p_dcreat   RADIOBUTTON GROUP g1.                "Directory Create"
PARAMETERS : p_ddel     RADIOBUTTON GROUP g1.                "Directory Delete"
PARAMETER : p_dir TYPE char100 MODIF ID m1 LOWER CASE.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF ( p_dcreat EQ 'X' OR p_ddel EQ 'X' ).
      IF screen-group1 EQ 'M1'.
        MOVE '1' TO screen-input.
        MOVE '0' TO screen-invisible.
        MODIFY SCREEN.
      ENDIF.
    ELSE.
      IF screen-group1 EQ 'M1'.
        MOVE '0' TO screen-input.
        MOVE '1' TO screen-invisible.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
  ENDLOOP.

START-OF-SELECTION.
  IF p_fsel EQ 'X'.
    MOVE abap_true TO lv_multi.
    MOVE 'Select the Files' TO lv_window_title.
    PERFORM file_select.
  ENDIF.
  IF p_fexist EQ 'X'.
    MOVE abap_true TO lv_multi.
    MOVE 'Select the Files' TO lv_window_title.
    PERFORM file_select.
    PERFORM file_existance_check.
  ENDIF.
  IF p_fattr EQ 'X'.
    MOVE abap_true TO lv_multi.
    MOVE 'Select the Files' TO lv_window_title.
    PERFORM file_select.
    PERFORM file_attributes.
  ENDIF.
  IF p_fcopy EQ 'X'.
    MOVE abap_false TO lv_multi.
    CLEAR : lv_file_from, lv_file_to.
    MOVE 'Select Source File to copy' TO lv_window_title.
    PERFORM file_select.
    MOVE 'Select Destination File' TO lv_window_title.
    PERFORM file_save.
    PERFORM file_copy.
  ENDIF.
  IF p_fdel EQ 'X'.
    MOVE abap_false TO lv_multi.
    CLEAR : lv_file.
    MOVE 'Select File to Delete' TO lv_window_title.
    PERFORM file_select.
    PERFORM file_delete.
  ENDIF.
  IF p_dsel EQ 'X'.
    MOVE 'Select the Directory' TO lv_window_title.
    PERFORM directory_select.
  ENDIF.
  IF p_dexist EQ 'X'.
    MOVE 'Select the Directory' TO lv_window_title.
    PERFORM directory_select.
    PERFORM directory_check.
  ENDIF.
  IF p_dflist EQ 'X'.
    MOVE 'Select the Directory' TO lv_window_title.
    PERFORM directory_select.
    PERFORM directory_list.
  ENDIF.
  IF p_dcreat EQ 'X'.
    MOVE p_dir TO lv_sel_folder.
    PERFORM directory_check.
    PERFORM directory_create.
  ENDIF.
  IF p_ddel EQ 'X'.
    MOVE p_dir TO lv_sel_folder.
    PERFORM directory_check.
    PERFORM directory_list.
    PERFORM directory_delete.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  file_select
*&---------------------------------------------------------------------*
FORM file_select.
  REFRESH : it_file_table.
  CLEAR   : rc.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = lv_window_title
      default_extension       = '*.txt'
      file_filter             = '*.txt'
      multiselection          = lv_multi
    CHANGING
      file_table              = it_file_table
      rc                      = rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF ( p_fcopy NE 'X' AND p_fdel NE 'X' ).
      WRITE :/ 'List of file(s) selected'.
      WRITE :/ '========================'.
      LOOP AT it_file_table INTO is_file_table.
        WRITE :/ is_file_table-filename.
      ENDLOOP.
    ELSEIF p_fcopy EQ 'X'.
      CLEAR : is_file_table.
      READ TABLE it_file_table INTO is_file_table INDEX 1.
      IF sy-subrc EQ 0.
        MOVE is_file_table-filename TO lv_file_from.
      ELSE.
        MESSAGE 'No File Selected for Copy.' TYPE 'I' DISPLAY LIKE 'E'.
        STOP.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "file_select

*&---------------------------------------------------------------------*
*&      Form  file_save
*&---------------------------------------------------------------------*
FORM file_save.
  CLEAR : lv_file_to, lv_path, lv_fullpath, lv_user_action.
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = lv_window_title
      default_extension    = '*.txt'
      prompt_on_overwrite  = 'X'
    CHANGING
      filename             = lv_file_to
      path                 = lv_path
      fullpath             = lv_fullpath
      user_action          = lv_user_action
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF lv_user_action = 9.        "user cancelled the action".
      MESSAGE 'No file Selected as Destination to copy' TYPE 'I' DISPLAY LIKE 'E'.
      STOP.
    ELSE.
      MOVE lv_fullpath TO lv_file_to.
    ENDIF.
  ENDIF.

ENDFORM.                    "file_save
*&---------------------------------------------------------------------*
*&      Form  file_existance_check
*&---------------------------------------------------------------------*
FORM file_existance_check.
  IF it_file_table[] IS NOT INITIAL.
    WRITE :/ 'Existance Check of Files'.
    WRITE :/ '========================'.
    LOOP AT it_file_table INTO is_file_table.
      CLEAR : lv_file, lv_result.
      MOVE is_file_table-filename TO lv_file.
      CALL METHOD cl_gui_frontend_services=>file_exist
        EXPORTING
          file                 = lv_file
        RECEIVING
          result               = lv_result
        EXCEPTIONS
          cntl_error           = 1
          error_no_gui         = 2
          wrong_parameter      = 3
          not_supported_by_gui = 4
          OTHERS               = 5.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
        IF lv_result = abap_true.
          CONDENSE lv_file.
          CONCATENATE lv_file 'Exists' INTO lv_file SEPARATED BY space.
        ELSE.
          CONCATENATE lv_file 'Not Exists' INTO lv_file SEPARATED BY space.
        ENDIF.
        WRITE :/ lv_file.
      ENDIF.
    ENDLOOP.
  ENDIF.
ENDFORM.                    "file_existance_check

*&---------------------------------------------------------------------*
*&      Form  file_attributes
*&---------------------------------------------------------------------*
FORM file_attributes.
  IF it_file_table[] IS NOT INITIAL.
    WRITE :/ 'File Attributes Display'.
    WRITE :/ '======================='.
    LOOP AT it_file_table INTO is_file_table.
      MOVE is_file_table-filename TO lv_file.
      CLEAR : lv_readonly, lv_normal, lv_hidden, lv_archive.
      CALL METHOD cl_gui_frontend_services=>file_get_attributes
        EXPORTING
          filename                   = lv_file
        IMPORTING
          readonly                   = lv_readonly
          normal                     = lv_normal
          hidden                     = lv_hidden
          archive                    = lv_archive
        EXCEPTIONS
          cntl_error                 = 1
          error_no_gui               = 2
          not_supported_by_gui       = 3
          wrong_parameter            = 4
          file_get_attributes_failed = 5
          OTHERS                     = 6.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
        WRITE :/ lv_file.
        WRITE :/ '--------------------------'.
        WRITE :/ 'Read Only : ' , lv_readonly.
        WRITE :/ 'Normal    : ' , lv_normal.
        WRITE :/ 'Hidden    : ' , lv_hidden.
        WRITE :/ 'Archive   : ' , lv_archive.
      ENDIF.

    ENDLOOP.
  ENDIF.
ENDFORM.                    "file_attributes

*&---------------------------------------------------------------------*
*&      Form  file_copy
*&---------------------------------------------------------------------*
FORM file_copy.
  IF lv_file_from IS NOT INITIAL AND lv_file_to IS NOT INITIAL.
    CALL METHOD cl_gui_frontend_services=>file_copy
      EXPORTING
        SOURCE               = lv_file_from
        DESTINATION          = lv_file_to
        overwrite            = space
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        disk_full            = 4
        access_denied        = 5
        file_not_found       = 6
        destination_exists   = 7
        unknown_error        = 8
        path_not_found       = 9
        disk_write_protect   = 10
        drive_not_ready      = 11
        not_supported_by_gui = 12
        OTHERS               = 13.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      MESSAGE 'File Copied' TYPE 'I'.
    ENDIF.
  ENDIF.
ENDFORM.                    "file_copy

*&---------------------------------------------------------------------*
*&      Form  file_delete
*&---------------------------------------------------------------------*
FORM file_delete.
  IF it_file_table[] IS NOT INITIAL.
    CLEAR : is_file_table, rc.
    READ TABLE it_file_table INTO is_file_table INDEX 1.
    IF sy-subrc EQ 0.
      CLEAR : lv_file.
      MOVE is_file_table-filename TO lv_file.
      CALL METHOD cl_gui_frontend_services=>file_delete
        EXPORTING
          filename             = lv_file
        CHANGING
          rc                   = rc
        EXCEPTIONS
          file_delete_failed   = 1
          cntl_error           = 2
          error_no_gui         = 3
          file_not_found       = 4
          access_denied        = 5
          unknown_error        = 6
          not_supported_by_gui = 7
          wrong_parameter      = 8
          OTHERS               = 9.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
        IF rc EQ 0.
          MESSAGE 'File deleted Successfully.' TYPE 'I'.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "file_delete

*&---------------------------------------------------------------------*
*&      Form  directory_select
*&---------------------------------------------------------------------*
FORM directory_select.
  CLEAR : lv_sel_folder.
  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title         = lv_window_title
    CHANGING
      selected_folder      = lv_sel_folder
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    WRITE :/ 'Selected Folder is ', lv_sel_folder.
  ENDIF.
ENDFORM.                    "directory_select

*&---------------------------------------------------------------------*
*&      Form  directory_check
*&---------------------------------------------------------------------*
FORM directory_check.
  IF lv_sel_folder IS NOT INITIAL.
    CLEAR : lv_result.
    CALL METHOD cl_gui_frontend_services=>directory_exist
      EXPORTING
        directory            = lv_sel_folder
      RECEIVING
        result               = lv_result
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      IF lv_result EQ abap_true.
        WRITE :/ 'Directory', lv_sel_folder,  'Exists'.
      ELSE.
        WRITE :/ 'Directory', lv_sel_folder, 'Not Exists'.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "directory_check

*&---------------------------------------------------------------------*
*&      Form  directory_list
*&---------------------------------------------------------------------*
FORM directory_list.
  IF lv_sel_folder IS NOT INITIAL.
    REFRESH : it_file_table.
    CLEAR : lv_file_count.
    CALL METHOD cl_gui_frontend_services=>directory_list_files
      EXPORTING
        directory                   = lv_sel_folder
        filter                      = '*.*'
        files_only                  = abap_false
        directories_only            = abap_false
      CHANGING
        file_table                  = it_file_table
        count                       = lv_file_count
      EXCEPTIONS
        cntl_error                  = 1
        directory_list_files_failed = 2
        wrong_parameter             = 3
        error_no_gui                = 4
        not_supported_by_gui        = 5
        OTHERS                      = 6.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      WRITE :/ 'No of Sub Directory/Files ', lv_file_count.
      WRITE :/ 'Directory List'.
      WRITE :/ '--------------'.
      LOOP AT it_file_table INTO is_file_table.
        WRITE :/ is_file_table-filename.
      ENDLOOP.
    ENDIF.
  ENDIF.
ENDFORM.                    "directory_list

*&---------------------------------------------------------------------*
*&      Form  directory_create
*&---------------------------------------------------------------------*
FORM directory_create.
  IF lv_sel_folder IS NOT INITIAL.
    CLEAR :rc.
    CALL METHOD cl_gui_frontend_services=>directory_create
      EXPORTING
        directory                = lv_sel_folder
      CHANGING
        rc                       = rc
      EXCEPTIONS
        directory_create_failed  = 1
        cntl_error               = 2
        error_no_gui             = 3
        directory_access_denied  = 4
        directory_already_exists = 5
        path_not_found           = 6
        unknown_error            = 7
        not_supported_by_gui     = 8
        wrong_parameter          = 9
        OTHERS                   = 10.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      IF rc EQ 0.
        WRITE :/ 'Directory ', lv_sel_folder, 'Created'.
      ELSE.
        WRITE :/ 'Directory ', lv_sel_folder, 'Not Created'.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "directory_create

*&---------------------------------------------------------------------*
*&      Form  directory_delete
*&---------------------------------------------------------------------*
FORM directory_delete.
  IF lv_sel_folder IS NOT INITIAL.
    IF lv_result EQ abap_true.
      IF lv_file_count EQ 0.
        CALL METHOD cl_gui_frontend_services=>directory_delete
          EXPORTING
            directory               = lv_sel_folder
          CHANGING
            rc                      = rc
          EXCEPTIONS
            directory_delete_failed = 1
            cntl_error              = 2
            error_no_gui            = 3
            path_not_found          = 4
            directory_access_denied = 5
            unknown_error           = 6
            not_supported_by_gui    = 7
            wrong_parameter         = 8
            OTHERS                  = 9.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ELSE.
          IF rc EQ 0.
            WRITE :/ 'Directory ', lv_sel_folder, 'Deleted'.
          ELSE.
            WRITE :/ 'Directory ', lv_sel_folder, 'Not Deleted'.
          ENDIF.
        ENDIF.
      ELSE.
        WRITE :/ 'Directory is not empty'.
      ENDIF.
    ELSE.
      WRITE :/ 'Directory Doesnt Exists'.
    ENDIF.
  ENDIF.
ENDFORM.                    "directory_delete

Selection Screen Snapshot