Skip to Content

How to get filename from selected directory

Helle experts,

I've a string variable from a directory with following content:

G:\SAP_R3_MRSS\SAP_MRS\FAVORIT.PDF

now I just want to select the filename, in this case FAVORIT.TXT

How can I do this ? The directory/filename may vary.

Some ideas ?

Thanks

Gerd

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 13, 2009 at 03:40 PM

    hi,

    Check this Fm TRINT_SPLIT_FILE_AND_PATH

    Function module              TRINT_SPLIT_FILE_AND_PATH
    
    Import parameters               Value
    
      FULL_NAME                       C:\KA51151\OTHERS\AUTH CREATION.PDF
    
    Export parameters               Value
    
      STRIPPED_NAME                   AUTH CREATION.PDF
      FILE_PATH                       C:\KA51151\OTHERS\

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 13, 2009 at 03:36 PM

    split the string into internal table, determine the number of lines of the internal table, the last line will be the name of the file:

    DATA : gv_lines TYPE i.
    SPLIT string INTO itab AT '\'.
    gv_lines = lines( itab ).
    READ TABLE itab INDEX gv_lines.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 13, 2009 at 03:38 PM

    one idea is to split the string at '\' or '/' and then check the splited componets ending with '.PDF' or '.TXT' or '.DOC' or anyother file type ....

    Thanks,

    Adi

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 13, 2009 at 04:10 PM

    Eric's split is probably the best way, but if you need a way to break the path into drive, path, and file, you could use a standard, well-used regular expression:

    PROGRAM zzfile.
    
    DATA:
       g_regex     TYPE string VALUE '\b((?#drive)[a-z]):\\((?#folder)[^/:*?"<>|\r\n]*\\)?((?#file)[^\\/:*?"<>|\r\n]*)',
       g_path_file TYPE string VALUE 'G:\SAP_R3_MRSS\SAP_MRS\FAVORIT.PDF',
       g_drive     TYPE string,
       g_path      TYPE string,
       g_file      TYPE string.
    
    
    FIND FIRST OCCURRENCE OF REGEX g_regex IN g_path_file SUBMATCHES g_drive g_path g_file IGNORING CASE.
    
    IF sy-subrc = 0.
    *  WRITE g_drive.
    *  WRITE g_path.
      WRITE g_file.
    ENDIF.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.