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

How to validate file path

Former Member
0 Likes
4,483

Hi,

I have field on my selection screen to enter file path. This parameter has F4 help to choose the file path. However, I have a concern over here...

If user enters file path manually(meaning that without using F4), there is chance that he may enter file path without any extension or without any drive/or the file which doesn't exist in the system ...In this case I want to validate the file path. And I should give an error saying "Invalid file path or name".

Could you please help me out with any FM or any other go for the same?

Thanks,

Sandeep

10 REPLIES 10
Read only

madhu_vadlamani
Active Contributor
0 Likes
2,379

Dear Sandeep,

Just hard code that and make as default.

Regards,

Madhu.

Read only

Former Member
0 Likes
2,379

Is it about validating in Application Server ? If yes, check the method OPEN in class CL_RSAN_UT_APPSERV_FILE_READER. Method returns sy-subrc 1 if there is an error in opening the specified file name.

If it is validation from GUI, check the methods "DIRECTORY_EXIST" and "FILE_EXIST" in class CL_GUI_FRONTEND_SERVICES.

Read only

Former Member
0 Likes
2,379

Hi Sandeep Reddy,

To validate the file path use the Function module

CV120_DOC_FILE_EXISTENCE_CHECK

It will check whether the file path you entered in the parameter PF_FILE exists or not.

Hope it helps.

Regards,

Phani Kumar M

Read only

Former Member
0 Likes
2,379

If you need to validate only the extension, you can do like the following code. Hopefully you must know the extension of the file.

filelen = strlen( filepath ).

filelen = filelen - 4 .

file_extn = filepath+filelen.

translate file_extn to upper case.

if file_extn ne '.csv'.

message 'Invalid file format.File extension should be '.CSV''.

endif.

Thanks & Regards,

Karthi

Read only

0 Likes
2,379

If its an application server simply you can check this

open dataset d1........

if sy-subrc 0.

write:/ 'File path not found'.

endif.

If sy-subrc is zero, it means no file exists.

else you can use a function module

FILE_GET_NAME_USING_PATH

Regards,

Uttam Agrawal

http://www.abapmadeeasy.com

Edited by: uttamagrawal on Feb 22, 2011 9:22 AM

Read only

Former Member
0 Likes
2,379

Dear Sandeep,

You can try the following approach:

1. In the declaration part of the parameter in Selection-screen, hard code the directory name, say 'C:\temp\'.

PARAMETERS: p_file TYPE localfile DEFAULT 'C:\temp\'.

2. Event: AT SELECTION-SCREEN OUTPUT.

Set the parameter to input disabled (Screen-input = 0). This way the user will not be able to manually change the value.

3. Event: AT SELECTION-SCREEN ON VALUE-REQUEST FOR <parameter_name>, make use of the method 'File_open_dialog' of the class 'CL_GUI_FRONTEND_SERVICES' to read the filename. Once you have the file name, you need to update the same on the screen. So make use of this function module, DYNP_VALUES_UPDATE.

Hope you find this useful.

Cheers!!!

Amit

Edited by: Amit Kumar Singh on Feb 3, 2011 9:20 AM

Read only

Former Member
0 Likes
2,379

Hi,

Use can use the folowing approach:

1. Get the file name using

- CALL FUNCTION 'F4_FILENAME' or

- CL_GUI_FRONTEND_SERVICES->GUI_UPLOAD

2. Validate the file path - pass the file name found in step 1 above

- CALL METHOD cl_gui_frontend_services=>file_exist

EXPORTING

file = lv_file

RECEIVING

result = lv_result.

IF lv_result NE c_x.

MESSAGE e114(zzsam). " Invalid file name entered. Display error message.

endif.

Read only

Former Member
0 Likes
2,379

Hi,

Use the Method DIRECTORY_EXIST in class CL_GUI_FRONTEND_SERVICES.

After the POP UP use this method. If the sy-subrc is zero then the filepath exists other wise no.

With Regards,

Sumodh.P

Read only

Former Member
0 Likes
2,379

HI

For the presentation server first you have to split the path entered into directory and file.You can used FM SO_SPLIT_FILE_AND_PATH for this purpose.

Now you can validate the directory and file independently using cl_gui_frontend_services=>directory_exist & cl_gui_frontend_services=>file_exist

*--FM to split File Path into Directory and File Name
  CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
    EXPORTING
      full_name     = v_fname
    IMPORTING
      stripped_name = v_file
      file_path     = v_dir
    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.

*--Method to Validate Directory
    CALL METHOD cl_gui_frontend_services=>directory_exist
      EXPORTING
        directory            = v_dir
      RECEIVING
        result               = lv_res
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.

    IF lv_res IS INITIAL.
      MESSAGE e000 WITH 'Directory does not exist'(010).
    ELSE.
      CLEAR lv_res.
*--Method to Validate File
      CALL METHOD cl_gui_frontend_services=>file_exist
        EXPORTING
          file                 = v_file
        RECEIVING
          result               = lv_res
        EXCEPTIONS
          cntl_error           = 1
          error_no_gui         = 2
          wrong_parameter      = 3
          not_supported_by_gui = 4
          OTHERS               = 5.

      IF lv_res IS INITIAL.
        MESSAGE e000 WITH 'File does not exist'(009).
      ENDIF.
    ENDIF.

For Application server you have to use open dataset only

OPEN DATASET v_fname FOR INPUT
                          IN TEXT MODE
                          ENCODING DEFAULT
                          IGNORING CONVERSION ERRORS.
    IF sy-subrc = 8.
      MESSAGE e000 WITH 'Invalid Directory or Filename'(015).
    ELSE.

Edited by: Rahuljn on Feb 3, 2011 3:27 PM

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,379

Sandeep,

Wy dont you search in SCN before posting. This has been discussed many times.