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 the a file path?

Former Member
0 Likes
7,587

Can anyone tell me the FM name to validate the a file/folder path? (C:\check\123.txt) in ABAP?

Regards,

Prathap

Edited by: Prathap on Mar 15, 2009 2:06 AM

1 ACCEPTED SOLUTION
Read only

I355602
Product and Topic Expert
Product and Topic Expert
3,419

Hi,

Use this code to validate the file path and then upload:-


PARAMETERS : p_file TYPE rlgrap-filename DEFAULT 'C:\TEST.XLS'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM f4_file_process USING p_file. "get F4 help for file

AT SELECTION-SCREEN.

  PERFORM validate_file_path USING p_file. "validate file path

START-OF-SELECTION.

  PERFORM upload_data. "code to upload your data into internal table

*&---------------------------------------------------------------------*
*&      Form  F4_FILE_PROCESS
*&---------------------------------------------------------------------*
*      -->P_FILE_PATH  text
*----------------------------------------------------------------------*
FORM f4_file_process USING p_file.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = 'P_FILE'
    IMPORTING
      file_name     = p_file.

  IF sy-subrc NE 0.
    MESSAGE e000(zsd).
  ENDIF.

ENDFORM.                    " F4_FILE_PROCESS
*&---------------------------------------------------------------------*
*&      Form  VALIDATE_FILE_PATH
*&---------------------------------------------------------------------*
*      -->P_FILE  text
*----------------------------------------------------------------------*
FORM validate_file_path USING p_file.

  DATA : lv_dir TYPE string,
         lv_file TYPE string,
         lv_result(1) TYPE c.

  DATA : lv_filename TYPE string.

  lv_filename = p_file.

  CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
    EXPORTING
      full_name     = p_file
    IMPORTING
      stripped_name = lv_file "file name
      file_path     = lv_dir "directory 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.


" check existence of directory
  CALL METHOD cl_gui_frontend_services=>directory_exist
    EXPORTING
      directory            = lv_dir
    RECEIVING
      result               = lv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF lv_result IS INITIAL. "lv_result is X if valid directory
    MESSAGE 'Invalid Directory' TYPE 'E'.
  ENDIF.

  CLEAR lv_result.

" check file existence
  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 = lv_filename
    RECEIVING
      result               = lv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF lv_result IS INITIAL. "lv_result is X if valid file path
    MESSAGE 'Invalid File' TYPE 'E'.
  ENDIF.

ENDFORM.                    " VALIDATE_FILE_PATH

Hope this helps you.

Regards,

Tarun

Can anyone tell me the FM name to validate the a file/folder path? (C:\check\123.txt) in ABAP?

Regards,

Prathap

Edited by: Prathap on Mar 15, 2009 2:06 AM

8 REPLIES 8
Read only

Former Member
0 Likes
3,419

Hi,

use the method

file_exist from the class cl_gui_frontend_services...

this will return you wether you have the file on the specified path or not...

Regards,

Siddarth

Read only

Former Member
0 Likes
3,419

I think the requirement is if the file exists in the location u specified on the selection screen:

If that is the case:

open dataset ( get right syntax (F1)

if sy-subrc = 0.

close dataset

else

write:/ 'check file'.

Read only

Former Member
0 Likes
3,419

check these,

u can use one of these

DX_FILE_EXISTENCE_CHECK

CV120_DOC_FILE_EXISTENCE_CHECK

CV122_DOC_FILE_EXISTENCE_CHECK

кu03B1ятu03B9к

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
3,419

Hi,

You can use method CL_GUI_FRONTEND_SERVICES=>FILE_EXIST

Or you can also refer FM's:

CONV_UTIL_CHECK_FILE_EXISTENCE

CV120_DOC_FILE_EXISTENCE_CHECK

CV122_DOC_FILE_EXISTENCE_CHECK

DX_FILE_EXISTENCE_CHECK

PFL_CHECK_OS_FILE_EXISTENCE

Or when you simply upload a file using any FM, just check sy-subrc. If not equal to 0, then display message Invalid Directory or File Name.

Hope this helps you.

Regards,

Tarun

Read only

Former Member
0 Likes
3,419

hi prathap , the sample code for validating the presentation and application server files.

&---------------------------------------------------------------------*
*& Form validate_server_file
*&---------------------------------------------------------------------*
FORM validate_server_file.
DATA: l_file TYPE tpfht-pffile.
CLEAR l_file.
l_file = p_file.
CALL FUNCTION 'PFL_CHECK_OS_FILE_EXISTENCE'
EXPORTING
fully_qualified_filename = l_file
IMPORTING
file_exists = l_true.
IF l_true = space.
flag1 = 'X'.
STOP.
ENDIF.
ENDFORM. " validate_server_file


*&---------------------------------------------------------------------*
*& Form validate_presentation_server
*&---------------------------------------------------------------------*
FORM validate_presentation_server .
DATA: result,
xfile TYPE string.
xfile = p_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = xfile
RECEIVING
result = result.
IF result NE 'X'.
flag1 = 'X'.
STOP.
ENDIF.
ENDFORM. "validate_presentation_server

thanks ,

chinnaiya

Read only

0 Likes
3,419

Ignore this message

Edited by: kartik tarla on Mar 15, 2009 1:11 PM

Read only

I355602
Product and Topic Expert
Product and Topic Expert
3,420

Hi,

Use this code to validate the file path and then upload:-


PARAMETERS : p_file TYPE rlgrap-filename DEFAULT 'C:\TEST.XLS'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM f4_file_process USING p_file. "get F4 help for file

AT SELECTION-SCREEN.

  PERFORM validate_file_path USING p_file. "validate file path

START-OF-SELECTION.

  PERFORM upload_data. "code to upload your data into internal table

*&---------------------------------------------------------------------*
*&      Form  F4_FILE_PROCESS
*&---------------------------------------------------------------------*
*      -->P_FILE_PATH  text
*----------------------------------------------------------------------*
FORM f4_file_process USING p_file.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = 'P_FILE'
    IMPORTING
      file_name     = p_file.

  IF sy-subrc NE 0.
    MESSAGE e000(zsd).
  ENDIF.

ENDFORM.                    " F4_FILE_PROCESS
*&---------------------------------------------------------------------*
*&      Form  VALIDATE_FILE_PATH
*&---------------------------------------------------------------------*
*      -->P_FILE  text
*----------------------------------------------------------------------*
FORM validate_file_path USING p_file.

  DATA : lv_dir TYPE string,
         lv_file TYPE string,
         lv_result(1) TYPE c.

  DATA : lv_filename TYPE string.

  lv_filename = p_file.

  CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
    EXPORTING
      full_name     = p_file
    IMPORTING
      stripped_name = lv_file "file name
      file_path     = lv_dir "directory 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.


" check existence of directory
  CALL METHOD cl_gui_frontend_services=>directory_exist
    EXPORTING
      directory            = lv_dir
    RECEIVING
      result               = lv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF lv_result IS INITIAL. "lv_result is X if valid directory
    MESSAGE 'Invalid Directory' TYPE 'E'.
  ENDIF.

  CLEAR lv_result.

" check file existence
  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 = lv_filename
    RECEIVING
      result               = lv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF lv_result IS INITIAL. "lv_result is X if valid file path
    MESSAGE 'Invalid File' TYPE 'E'.
  ENDIF.

ENDFORM.                    " VALIDATE_FILE_PATH

Hope this helps you.

Regards,

Tarun

Read only

Former Member
0 Likes
3,419

Hi,

One of the methods is,

data: w_result type abap_bool.

  call method cl_gui_frontend_services=>file_exist
    exporting
      file            = p_file "File name
    receiving
      result          = w_result "Result
    exceptions
      cntl_error      = 1
      error_no_gui    = 2
      wrong_parameter = 3
      others          = 4.

if w_result eq 'X'.
"File exists
else.
"File not exists
endif.

Regards,

Mannoj Kumar P