Application Development 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: 

Obtaining the current directory of a file

Former Member
0 Kudos

Hi experts,

i've got the following problem.

I have a file in my program, and i've to create another file in the same directory of the first one, doesn't matter if the file is on the application server or is a local file. ' want to know how can i obtain the current directory of the first file.

Who can help me?

Thanks to all.

Ivan

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Do something like this.....




  DATA: lv_slash   TYPE c value '\'.
  DATA: lv_docname TYPE string value 'C:\this_directory\that_directory\test_File.xml'.

* Reverse the string
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = lv_docname
      lang      = sy-langu
    IMPORTING
      rstring   = lv_docname
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.


* look for the first slash in reversed path/filename
* and trim it off.
  SEARCH lv_docname FOR lv_slash.
  IF sy-subrc = 0.
    SHIFT lv_docname LEFT BY sy-fdpos PLACES.
  ENDIF.

* reverse the string again to reveal the parent directory
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = lv_docname
      lang      = sy-langu
    IMPORTING
      rstring   = lv_docname
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.


Regards.

Rich Heilman

7 REPLIES 7

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Do something like this.....




  DATA: lv_slash   TYPE c value '\'.
  DATA: lv_docname TYPE string value 'C:\this_directory\that_directory\test_File.xml'.

* Reverse the string
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = lv_docname
      lang      = sy-langu
    IMPORTING
      rstring   = lv_docname
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.


* look for the first slash in reversed path/filename
* and trim it off.
  SEARCH lv_docname FOR lv_slash.
  IF sy-subrc = 0.
    SHIFT lv_docname LEFT BY sy-fdpos PLACES.
  ENDIF.

* reverse the string again to reveal the parent directory
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = lv_docname
      lang      = sy-langu
    IMPORTING
      rstring   = lv_docname
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.


Regards.

Rich Heilman

0 Kudos

No, sorry perhaps i've not been clear...

I want to create a file in the same directory of another file. How can i do this?

Thks to all

Ivan

0 Kudos

Hi,

you can retrieve the directory of the file using this FM:

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

logical_filename = w_nom " logical file name

parameter_1 = w_numlot1

IMPORTING

file_name = w_fichier " full path of file

EXCEPTIONS

file_not_found = 1

OTHERS = 2.

Regards,

Sooness

Edited by: Sooness Munogee on Jan 22, 2008 10:06 AM

0 Kudos

Hi Ivano,

Check FM's

SO_SPLIT_FILE_AND_PATH

SO_FILENAME_GET_WITH_PATH

PC_SPLIT_COMPLETE_FILENAME

Thanks

Lakshman

0 Kudos

Thanks Rich,

your solution is really helpful.

Regards,

Sud.

Former Member
0 Kudos

Hi Ivano,

That will be very tricky as you will need to search the filename on your presentation layer. (in other words you will need to deploy file search in your ABAP )..

I Don't know complete scenario this is involved with, maybe you can try using DIRECTORY_SET_CURRENT when you get this filename and set the directory as your working directory and use DIRECTORY_GET_CURRENT from class CL_GUI_FRONTEND_SERVICES when finding the working directory..

Refere to other methods of CL_GUI_FRONTEND_SERVICES, it might be helpful.

Regards,

Mohaiyuddin

Former Member
0 Kudos

Ok problem solved, thanks to all