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: 

Validating directory and file

Former Member
0 Kudos

Hello All,

I want to check if user had input correct directory or a file name. I mean, if user type in wrong directory, for example, instead of typing "c:\file.txt", if they type "z:\file.txt"

I should let the user know that the file or directory does not exist. Is there any FM for that? Please let me know.

Secondly, is there any way I can write down the field names of my internal table while try to download the internal table to a txt file. How can i get this info in my txt file from my internal table. Right now, I can only download the content of my internal table to a txt file, without field headings. Any help would be great.

Thanks in advanced!

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

You can use this FM

  DATA: TBL_FILES LIKE SDOKPATH OCCURS 10 WITH HEADER LINE,
        TBL_DIRS LIKE SDOKPATH OCCURS 10 WITH HEADER LINE.


  CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
       EXPORTING
            DIRECTORY  = P_DIRECTORY
       TABLES
            FILE_TABLE = TBL_FILES
            DIR_TABLE  = TBL_DIRS.
describe table tbl_dirs lines sy-index.
if sy-index is intial.
* message.. directory does not exits
endif.

Regards,

Naimesh Patel

13 REPLIES 13

naimesh_patel
Active Contributor
0 Kudos

You can use this FM

  DATA: TBL_FILES LIKE SDOKPATH OCCURS 10 WITH HEADER LINE,
        TBL_DIRS LIKE SDOKPATH OCCURS 10 WITH HEADER LINE.


  CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
       EXPORTING
            DIRECTORY  = P_DIRECTORY
       TABLES
            FILE_TABLE = TBL_FILES
            DIR_TABLE  = TBL_DIRS.
describe table tbl_dirs lines sy-index.
if sy-index is intial.
* message.. directory does not exits
endif.

Regards,

Naimesh Patel

Former Member
0 Kudos

Hi,

Check Class CL_GUI_FRONTEND_SERVICES.

It has all the methods related to frontend/file processing.

Regards,

Atish

Former Member
0 Kudos

Hi,

Use like this

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME.
PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN: END OF BLOCK b2.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  DATA: directory TYPE string,
        filetable TYPE filetable,
        line      TYPE LINE OF filetable,
        rc        TYPE i.
  CALL METHOD cl_gui_frontend_services=>get_temp_directory
    CHANGING
      temp_dir = directory.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title      = 'SELECT THE FILE'
      initial_directory = directory
      file_filter       = '*.XLS'
      multiselection    = ' '
    CHANGING
      file_table        = filetable
      rc                = rc.
  IF rc = 1.
    READ TABLE filetable INDEX 1 INTO line.
    p_file = line-filename.
  ENDIF.

Reward if this helps,

Satish

Former Member
0 Kudos

if you are using gui_upload or gui_download ,these FM give error message if you give wrong path.

fo your 2nd query try this.

let itab has two fields.filed1 and field2.

before filling data into itab ,append field names to it.

itab1-field1 = 'FIELD1'.

itab1-filed2 = 'FIELD2'.

APPEND ITAB1.

THIS WILL BE THE FIRST RECORD.

then fill the itab with data.

USE GUI_DOWNLAOD TO transfer data from prgram to presentation server

ferry_lianto
Active Contributor
0 Kudos

Hi,

You can use the DIRECTORY_EXIST method to check file path for both, PC frontend and application server.


type-pools: abap.
 
data: rc type abap_bool.
data: dir type string.
 
parameters: p_file type localfile default 'C:'.
 
 
start-of-selection.
 
dir = p_file.
 
  call method cl_gui_frontend_services=>directory_exist
    exporting
      directory            = dir
    receiving
      result               = rc
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      others               = 5.
 
  if rc = 'X'.
 
    write:/ 'The directory does exist'.
 
  else.
 
    write:/ 'The directory does not exist'.
 
  endif.

Also you can use FM GUI_DOWNLOAD to download internal table with fieldname(s).


data: begin OF itab occurs 0,
        matnr like mara-matnr,
      end of itab.

data : begin of it_fieldnames occurs 0,
        name(100),
       end of it_fieldnames.

it_fieldnames-name = 'MATNR'.
append it_fieldnames.

select matnr from mara into table itab UP TO 10 ROWS.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME = 'C:Documents and SettingssampathDesktopflatfile.XLS'
    FILETYPE = 'ASC'
  TABLES
    DATA_TAB = itab
    FIELDNAMES = IT_FIELDNAMES .

Regards,

Ferry Lianto

Former Member
0 Kudos

Hi,

go thru this sample program...any way in GUI_DWONLOAD if file is not found SY-SUBRC value will set to 19..


TABLES : mara.
DATA: ld_filename TYPE string VALUE 'C:TEMPDATA.XLS'.

DATA: BEGIN OF itab OCCURS 0,
matnr LIKE mara-matnr,
ERSDA LIKE mara-ersda,
END of itab.

DATA: BEGIN OF fl_name OCCURS 0,
name LIKE mara-matnr,
END of fl_name.
fl_name-name = 'Material'.
APPEND fl_name.
fl_name-name = 'Creation date'.
APPEND fl_name.



SELECT matnr ersda FROM mara INTO TABLE itab up to 20 rows.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename                        = ld_filename
   WRITE_FIELD_SEPARATOR           = ';'
  tables
    data_tab                        = itab[]
    FIELDNAMES                      = fl_name
 EXCEPTIONS
   FILE_WRITE_ERROR                = 1
   NO_BATCH                        = 2
   GUI_REFUSE_FILETRANSFER         = 3
   INVALID_TYPE                    = 4
   NO_AUTHORITY                    = 5
   UNKNOWN_ERROR                   = 6
   HEADER_NOT_ALLOWED              = 7
   SEPARATOR_NOT_ALLOWED           = 8
   FILESIZE_NOT_ALLOWED            = 9
   HEADER_TOO_LONG                 = 10
   DP_ERROR_CREATE                 = 11
   DP_ERROR_SEND                   = 12
   DP_ERROR_WRITE                  = 13
   UNKNOWN_DP_ERROR                = 14
   ACCESS_DENIED                   = 15
   DP_OUT_OF_MEMORY                = 16
   DISK_FULL                       = 17
   DP_TIMEOUT                      = 18
   FILE_NOT_FOUND                  = 19
   DATAPROVIDER_EXCEPTION          = 20
   CONTROL_FLUSH_ERROR             = 21
   OTHERS                          = 22
          .
IF sy-subrc EQ 19.
WRITE 😕 'FILE NOT FOUND'.
ENDIF.


Former Member
0 Kudos

Take a look at this sample code.

DATA: BEGIN OF t_mara OCCURS 0,
        matnr TYPE mara-matnr,
        prdha TYPE mara-prdha,
      END OF t_mara,

      BEGIN OF t_header OCCURS 0,
        field(20) TYPE c,
      END OF t_header,

      w_dir    TYPE string         ,
      w_file   TYPE string         ,
      w_target TYPE rlgrap-filename,
      w_result TYPE c              ,
      w_len    TYPE i              .


PARAMETERS: p_dir(50)  TYPE c OBLIGATORY,
            p_file(50) TYPE c OBLIGATORY.


AT SELECTION-SCREEN.

  w_dir  = p_dir.
  w_file = p_file.

  w_len = STRLEN( w_dir ).

  SUBTRACT 1 FROM w_len.

  CASE w_dir+w_len(1).
    WHEN ''.
      w_dir = w_dir+0(w_len).
  ENDCASE.

  CLEAR w_result.

* Check for Valid Directory
  CALL METHOD cl_gui_frontend_services=>directory_exist
    EXPORTING
      directory = w_dir
    RECEIVING
      result    = w_result
    EXCEPTIONS
      OTHERS    = 0.

  CASE w_result.
    WHEN space.
      MESSAGE e208(00) WITH 'Directory does not exist'.
  ENDCASE.


START-OF-SELECTION.

* Get data to be downloaded
  SELECT matnr
         prdha
    UP TO 10 ROWS
    FROM mara
    INTO TABLE t_mara.

  CLEAR w_target.

  CONDENSE: w_dir ,
            w_file.

* Create target location
  CONCATENATE w_dir
              ''
              w_file
    INTO w_target.

* Column Headers
  REFRESH t_header.

  t_header-field = 'Material Number'.
  APPEND t_header.

  t_header-field = 'Product Hierarchy'.
  APPEND t_header.

  CLEAR t_header.

* Download the file
  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      filename   = w_target
      filetype   = 'DAT'
    TABLES
      data_tab   = t_mara
      fieldnames = t_header
    EXCEPTIONS
      OTHERS     = 0.

Let me know if this helps.

Former Member
0 Kudos

Hello All,

Thanks to all for their very good help! I was able to validate the directory/file name just using the gui_download method.

But I am still not able to put the field names in my txt file. I created separate header table and passing it to the gui_download. But for some reason it is not printing out the field names. Here it what I am trying to do:

DATA: BEGIN OF ilogtab occurs 0, "saving the log file structure

log_f(256) TYPE c,

END OF ilogtab.

DATA: BEGIN OF it_fieldnames occurs 0, "saving the log file structure

l_f(256) TYPE c,

END OF it_fieldnames.

*DATA ilogtab LIKE struct3_logf OCCURS 0 WITH HEADER LINE.

I am concatenating fields into a string and than appending it to the internal tables.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = f1

filetype = 'ASC'

write_field_separator = 'X'

append = ''

TABLES

data_tab = ierrtab "msg_error

fieldnames = it_fieldnames

EXCEPTIONS

file_open_error = 1

file_write_error = 2

invalid_filesize = 3

invalid_table_width = 4

invalid_type = 5

no_batch = 6

unknown_error = 7

OTHERS = 8.

IF sy-subrc <> 0.

MESSAGE e999 WITH 'Error File Write Error: ' sy-subrc.

ENDIF.

ENDFORM.

Any input on that please.

Thanks in advanced.

Note: field contents are getting print in txt file, but with out field names.

0 Kudos

Hi,

There is a parameter in GUI_DOWNLOAD which tells to write the file header. I can't tell the name as I am not in front of system, but if you just look the importing parameters of the FM you will find it.

Regards,

Atish

0 Kudos

J Are

1) I have found that GUI_DOWNLOAD does not download field names correctly. Use WS_DOWNLOAD instead.

2) Take a look at my code in my previous post to this thread. That code works. Try implementing it for your scenario.

null

Former Member
0 Kudos

One mistake....I am passing the ilogtab to the gui_download...not the ierrtab. I cut and paste the wrong FORM. Sorry about that.

0 Kudos

Have you fill the Fieldname tab as:

it_fieldnames = 'Field1'.
Append it_fieldnames.

it_fieldnames = 'Field2'.
append it_fieldnames.

Regards,

Naimesh Patel

Former Member
0 Kudos

Thanks to all who helped! Problem is solved. I achieved the issue by creating two tables one for the fields and one for the data...thanks AGain...