Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Get filename from a fullpath

former_member246786
Participant
31,049

Hi,

I would like to know if there is a function module that can extract a filename from a fullpath with extension.

It's not really difficult to do such a form by myself but I would like to know if it already exist in SAP standard functions.

Regards,

Morgan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
11,175

Hi morgan,

1. PC_SPLIT_COMPLETE_FILENAME

2. By giving the full path to the FM,

we get

a) file name (only file name)

b) also we can get the extension separately in other parameter

regards,

amit m.

Hi,

I would like to know if there is a function module that can extract a filename from a fullpath with extension.

It's not really difficult to do such a form by myself but I would like to know if it already exist in SAP standard functions.

Regards,

Morgan

10 REPLIES 10
Read only

Former Member
0 Likes
11,176

Hi morgan,

1. PC_SPLIT_COMPLETE_FILENAME

2. By giving the full path to the FM,

we get

a) file name (only file name)

b) also we can get the extension separately in other parameter

regards,

amit m.

Read only

0 Likes
11,175

Thank you Amit but a problem remain : I got the filename but only the 12 first characters and i want the entire filename without extension, is it possible to have it using this FM?

regards,

Morgan

Read only

0 Likes
11,175

Hi,

Use the below method.

CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG.


DATA: V_FILENAME   TYPE STRING,
      IT_FILETABLE TYPE FILETABLE,
      V_RC         TYPE I.

    CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
      EXPORTING
        WINDOW_TITLE            = 'File Open'
        INITIAL_DIRECTORY       = 'C:'
      CHANGING
        FILE_TABLE              = IT_FILETABLE
        RC                      = V_RC
      EXCEPTIONS
        FILE_OPEN_DIALOG_FAILED = 1
        CNTL_ERROR              = 2
        ERROR_NO_GUI            = 3
        OTHERS                  = 4.

    IF SY-SUBRC = 0.
      READ TABLE IT_FILETABLE INDEX 1 INTO V_FILENAME.
    ELSE.
      MESSAGE E100 WITH TEXT-011. " Error while opening the file
    ENDIF.

Hope this code will help you. Don't forget to reward if it is helpful for you

Thanks,

GSK

Read only

Former Member
0 Likes
11,175

Hi Morgan,

Plese check this method CL_GUI_FRONTEND_SERVICES->FILE_OPEN_DIALOG.

CALL METHOD cl_gui_frontend_services=>file_open_dialog
  EXPORTING
    window_title = 'Import from a local file'
    default_extension = 'DAT'
* DEFAULT_FILENAME =
    initial_directory = 'C:'
* MULTISELECTION =
* WITH_ENCODING =
  CHANGING
    file_table = l_table
    rc = l_rc
* USER_ACTION =
* FILE_ENCODING =
  EXCEPTIONS
    file_open_dialog_failed = 1
    cntl_error = 2
    error_no_gui = 3
    not_supported_by_gui = 4
    others = 5.

Hope this will help.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
11,175

Hi

Try this code..

Regards,

Raj

  • Find the local directory

l_loc = 1.

WHILE sy-subrc EQ 0.

SEARCH p_ofile FOR '/' STARTING AT l_loc.

IF sy-fdpos GT 0.

ADD sy-fdpos TO l_loc.

l_cmd = p_ofile(l_loc).

l_lng = 60 - l_loc.

l_fil = p_ofile+l_loc(l_lng).

ELSE.

ADD 1 TO l_loc.

ENDIF.

ENDWHILE.

You will get the local directory in l_cmd. (So the remaining part of p_ofile is the file)

Read only

Former Member
11,175

Hi Morgan,

Use this Function Module SO_SPLIT_FILE_AND_PATH.

REPORT ZTEST_SHAIL4 .

data: file_name type RLGRAP-FILENAME.

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

EXPORTING

full_name = 'D:\SP\kun.txt'

IMPORTING

STRIPPED_NAME = file_name

  • FILE_PATH =

EXCEPTIONS

X_ERROR = 1

OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

message i001(zmess) with file_name.

Regards,

SP.

Read only

Former Member
0 Likes
11,175

hi,

We can get the file name from full path by writing a few lines of code. In case of function module, there can be a limitation to the number of characters of file name.

try this code:

data: v_str(50) value 'c:/first/second/myfile.doc'.

types : begin of t_itab,
          record like v_str,
        end of t_itab.

data: itab type table of t_itab with header line,
      v_lines type i.

start-of-selection.

  split v_str at '/' into table itab.
  describe table itab lines v_lines.
  read table itab index v_lines.
  if sy-subrc = 0.
    write :/ 'File name :', itab-record.
  endif.

Regards,

Sailaja.

Read only

0 Likes
11,175

HI all,

thanks for you're help

I can't use the open file dialog cause it will display a popup and I don't want to prompt user for filename, just to extract filename without extension from a fullpathy I already have.

I will try other FM modules you gave me. Conerning the algoritm , I don't want to use it cause I alredy have one, just wanted to know if there is standard FM that do it but I thank you very much

regards,

Morgan

Read only

11,175

Hi,

the following function modules are useful:

CV120_SPLIT_PATH

CV120_SPLIT_FILE

Good luck

  Reinhard

Read only

Former Member
0 Likes
11,175

This could be done using regexp (source full path in lv_fullpath)


data: lv_root type string, lv_path type string, lv_filename type string, lv_ext type string.

         " Should match both windows frontend and application server paths

         find REGEX '^(.:\\|\/\/)(.+\\|.+\/)*(.+)\.(.+)' in lv_fullpath SUBMATCHES lv_root lv_path lv_filename lv_ext.

Result would be stored in corresponding variables.

For example:

lv_fullpath = 'c:\temp\test\filename.txt''

result:

variabledata
lv_rootc:\
lv_pathtemp\test
lv_filenamefilename
lv_exttxt