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

file

Former Member
0 Likes
611

Suppose I have a file stored in a particular directory in say PC, then how do I

(1) Validate if file path exists

(2) Fetch data from file of file path exists.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
589

Hello,

Check this.


data: file type string,
      result(1) type c.
 
 
start-of-selection.
 
  file = 'C:/abaps/test.txt'.
 
  call method cl_gui_frontend_services=>file_exist
    exporting
      file            = file
    receiving
      result          = result.
 
 
  if result = 'X'.
    write:/ 'file does exist'.
  else.
    write:/ 'file not found'.
  endif.

If useful reward.

Vasanth

4 REPLIES 4
Read only

Former Member
0 Likes
590

Hello,

Check this.


data: file type string,
      result(1) type c.
 
 
start-of-selection.
 
  file = 'C:/abaps/test.txt'.
 
  call method cl_gui_frontend_services=>file_exist
    exporting
      file            = file
    receiving
      result          = result.
 
 
  if result = 'X'.
    write:/ 'file does exist'.
  else.
    write:/ 'file not found'.
  endif.

If useful reward.

Vasanth

Read only

Former Member
0 Likes
589

Sumit,

For presentation server ( desktop,pc ) you may use the FM GUI_UPLOAD

where u need to give the pathname of the file .

This does not validate but serves both 1 and 2 , sought by you .

If the pathname/ file name is wrong, the FM throws exception .

~ Laxmi

  • Reward all helpful answers

Read only

varma_narayana
Active Contributor
0 Likes
589

Hi.

I think you can try out with this code

PARAMETERS : p_file type rlgrap-filename .

DATA : V_rETURN TYPE I.

**To generate F4 help to select the file path

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

CALL FUNCTION 'F4_FILENAME'

**Validation whether filep path exists

AT SELECTION-SCREEN ON P_FILE.

CALL FUNCTION 'WS_QUERY'

EXPORTING

FILENAE = P_FILE

QUERY = 'FE' "FILE EXISTS

IMPORTING

RETURN = V_RETURN .

IF V_RETURN = 0.

MESSAGE 'FILE DOES NOT EXIST' TYPE 'E'.

ENDIF.

Hope you will get the solution.

Reward for helpful answer.

Read only

Former Member
0 Likes
589

Hi,

To validate file path:

FORM check_file_path .

  • To Check the file path

IF pa_upd EQ gc_x.

SEARCH pa_file FOR gc_csv.

IF sy-subrc NE gc_zero_num.

MESSAGE i019.

LEAVE LIST-PROCESSING.

ENDIF.

ENDIF.

ENDFORM. " check_file_path

To upload file:

FORM upload_csv_file .

CLEAR gv_file.

gv_file = pa_file.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = gv_file

filetype = gc_asc

has_field_separator = gc_x

TABLES

data_tab = gt_dummy

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

IF sy-subrc NE gc_zero_num.

MESSAGE i006.

LEAVE LIST-PROCESSING.

ENDIF.

  • Check if the input file is blank

IF gt_dummy[] IS INITIAL.

MESSAGE i007.

LEAVE LIST-PROCESSING.

ENDIF.

Regards

Kannaiah