5 1 1 1 1 1 Rating 100% (1 Vote)

I guess all of us often had to work with files in ABAP usign some of available FM like 'GUI_UPLOAD', 'GUI_DOWNLOAD' etc. When doing so sometimes there is a need to display in ALV the filename with or without extension . There are plenty of FM that helps to do so but the have one disadvantage -> they base on 3 digit extension and sometimes they have limited path size.

But where stadard SAP cannot help you, then you have to helpyourself with ABAP. There is a really simple way to split filename/extension without any restrictions known from standard FM. Here is a code for a method, but you can use it also to create form or FM. 

Importing:

VALUE( I_PATH ) TYPE CSEQUENCE -> full path

Exporting:

E_FILENAME TYPE CSEQUENCE -> filename without extension
E_FILENAMEFULL TYPE CSEQUENCE -> filename with extension
E_EXTENSION TYPE CSEQUENCE -> extension

Main code:

method get_filename_from_path.

  datam_offset type i.
  datam_path type string.
  datam_filename type string.
  datam_length type i.
  datam_size type i.

  clear m_offset.
  m_path i_path.
  find all occurrences of '\' in m_path in character mode
  match offset m_offset.

  if m_offset ne 0.
    add to m_offset.
    shift m_path by m_offset places left.
  endif.

  if e_filenamefull is requested.
    e_filenamefull m_path.
  endif.
  if e_filename is requested or e_extension is requested.
    m_filename     m_path.
  endif.
  clear m_offset.

  find all occurrences of '.' in m_filename in character mode
  match offset m_offset.

  m_length strlenm_filename ).

  if m_offset ne 0.
    e_filename m_filename+0(m_offset).
    if e_extension is requested.
      add to m_offset.
      m_size m_length m_offset.
      e_extension m_filename+m_offset(m_size).
    endif.
  else.
    e_filename m_filename.
  endif.

endmethod.

As said before easy and fast.