[SOLVED] Check if a file exists faster than OPEN DATASET

Issue

I’m searching for a way to check if a file exists before using the OPEN DATASET command to open it. The OPEN DATASET command takes up to 30 seconds to trigger an exception, which is too slow for my liking.

This is the code:

TRY.
  OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
                       ENCODING DEFAULT
                       WITH SMART LINEFEED.

  CONCATENATE ` ` lv_resultdata INTO lv_resultdata.

  TRANSFER lv_resultdata TO lv_file.
  CLOSE DATASET lv_file.

CATCH cx_sy_file_access_error.

MESSAGE 'Placeholder-message. File cannot be reached'.
EXIT.
ENDTRY.

Solution

Try this:

DATA: filepath TYPE epsf-epsdirnam VALUE '/tmp'.

CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
  EXPORTING
    dir_name               = filepath
    file_mask              = 'somefile.txt'
  EXCEPTIONS
    invalid_eps_subdir     = 1
    sapgparam_failed       = 2
    build_directory_failed = 3
    no_authorization       = 4
    read_directory_failed  = 5
    too_many_read_errors   = 6
    empty_directory_list   = 7
    OTHERS                 = 8.

CHECK sy-subrc = 0.

" writing dataset

It can also be used for remote servers.

Answered By – Suncatcher

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *