SAP ABAP function module to get filename and extension

 As sap has provide many function module to get filename and extension by passing the filename as input some FM as :

PC_CHECK_FILENAME_WITH_EXT

TRINT_FILE_GET_EXTENSION

PC_SPLIT_COMPLETE_FILENAME

there are many more FM available, but i was facing following 2 main issues.

1: In case of multiple dots '.' in filename , extension was not coming properly as in sap some file get generated with filename as date.

2: System was not checking whether that file actual exist or not at the given location.

To overcome this problem, I have  created custom FM.



FUNCTION ZGET_FILENAME_WITH_EXT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IP_FULL_FILENAME) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EP_FILENAME) TYPE  STRING
*"     VALUE(EP_EXTENSION) TYPE  CHAR20
*"     VALUE(EP_MESSAGE) TYPE  CHAR100
*"     VALUE(EP_RET_CODE) TYPE  CHAR1
*"----------------------------------------------------------------------

types begin of tp_split,
         data(500),
        end of tp_split.

 data gt_files type STANDARD TABLE OF file_table,
        gs_files type file_table,
        gt_split type STANDARD TABLE OF tp_split,
        gs_split type tp_split.

DATA l_ret(1)   TYPE c,
       gv_count type i,
       gv_filename type string,
       gv_path type string.

"check file exist or not
  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 ip_full_filename
    receiving
      result               l_ret
    EXCEPTIONS
      cntl_error           1
      error_no_gui         2
      wrong_parameter      3
      not_supported_by_gui 4
      others               5
          .
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

     if l_ret is INITIAL.
        ep_ret_code  1.
        ep_message   'File does not exist'.
        exit.
     endif.

"split the file into path and filename, to get directory

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
  EXPORTING
    full_name           ip_full_filename
 IMPORTING
   STRIPPED_NAME       gv_filename
   FILE_PATH           gv_path
 EXCEPTIONS
   X_ERROR             1
   OTHERS              2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

 if gv_filename is not initial and gv_path is not initial.

      split gv_filename at '.' into table gt_split.

      clear gv_count.
      describe table gt_split lines gv_count.

      try.
       ep_extension gt_split[ gv_count ]-data.
      catch cx_root.
      endtry.

      condense ep_extension.

      ep_filename   gv_filename.
      ep_ret_code   0.

 else.
     ep_ret_code  1.
     ep_message   'File does not exist'.
     exit.
 endif.

ENDFUNCTION.


Post a Comment

0 Comments

Total Pageviews