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: 

Checking Correct Filepath and file type

Former Member
0 Kudos

Hi,

I have a requirement where in i need to check if the filepath entered by the user is valid, the filepath is where the internal table that i created in will be downloaded using GUI_DOWNLOAD.

I want to know if the directory exist: like C: or E:

and the file is in excel format.. .xls

Thanks

Edited by: Alvin Rosales on Apr 7, 2009 5:18 AM

1 ACCEPTED SOLUTION

Sougata
Active Contributor
0 Kudos

Hi Alvin,

For checking whether the file path is valid, you can use the code as below:


  call method cl_gui_frontend_services=>directory_exist
    exporting
      directory       = im_dirname
    receiving
      result          = x_directory
    exceptions
      cntl_error      = 1
      error_no_gui    = 2
      wrong_parameter = 3
      others          = 4.

case sy-subrc.
 when 1.
" your exception handling here
 when 2.
.....
endcase.

For checking the file format entered by the user, you can force the user to enter 'xls' after the dot (.) in the filename parameter itself on event AT SELECTION-SCREEN...

After you've validated the above you can use code below to download internal table data to the specified directory:


      call method cl_gui_frontend_services=>gui_download
        exporting   bin_filesize            = p_bin_filesize
                    filename                = l_fullpath
                    filetype                = p_filetype
                    write_lf                = p_write_lf
                    trunc_trailing_blanks   = p_trunc_blanks
                    trunc_trailing_blanks_eol = p_trunc_blanks_eol
                    append                  = p_append
                    codepage                = p_codepage
        importing   filelength              = p_filelength
        changing    data_tab                = <l_data_tab>
        exceptions  file_write_error        = 1
                    no_batch                = 2
                    gui_refuse_filetransfer = 3
                    invalid_type            = 4
                    unknown_error           = 5
                    others                  = 6.

Hope this helps,

Cheers,

Sougata.

10 REPLIES 10

Former Member
0 Kudos

Hi,

You can do validations using

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

CALL METHOD cl_gui_frontend_services=>directory_exist

CALL METHOD cl_gui_frontend_services=>file_exist

Regards,

jaya

0 Kudos

Hi Jayapradha,

I use the FM and splitted the directory and the file name.. how can i use the call methods? Thanks

babu_kilari4
Active Contributor
0 Kudos

Hi Alvin,

Try to use the FM FILE_GET_NAME_USING_PATH and do manipulations with the help of sy-subrc value given by it

Sougata
Active Contributor
0 Kudos

Hi Alvin,

For checking whether the file path is valid, you can use the code as below:


  call method cl_gui_frontend_services=>directory_exist
    exporting
      directory       = im_dirname
    receiving
      result          = x_directory
    exceptions
      cntl_error      = 1
      error_no_gui    = 2
      wrong_parameter = 3
      others          = 4.

case sy-subrc.
 when 1.
" your exception handling here
 when 2.
.....
endcase.

For checking the file format entered by the user, you can force the user to enter 'xls' after the dot (.) in the filename parameter itself on event AT SELECTION-SCREEN...

After you've validated the above you can use code below to download internal table data to the specified directory:


      call method cl_gui_frontend_services=>gui_download
        exporting   bin_filesize            = p_bin_filesize
                    filename                = l_fullpath
                    filetype                = p_filetype
                    write_lf                = p_write_lf
                    trunc_trailing_blanks   = p_trunc_blanks
                    trunc_trailing_blanks_eol = p_trunc_blanks_eol
                    append                  = p_append
                    codepage                = p_codepage
        importing   filelength              = p_filelength
        changing    data_tab                = <l_data_tab>
        exceptions  file_write_error        = 1
                    no_batch                = 2
                    gui_refuse_filetransfer = 3
                    invalid_type            = 4
                    unknown_error           = 5
                    others                  = 6.

Hope this helps,

Cheers,

Sougata.

Former Member
0 Kudos

Hi Sougata,

Thanks for the reply just additional question in using the

CALL METHOD cl_gui_frontend_services=>directory_exist

EXPORTING

directory = g_path -


> will this be the filepath directory (example: C:\ )

RECEIVING

result = x_directory -


> what type of data will this be?

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

wrong_parameter = 3

OTHERS = 4.

sorry for the followup question

awin_prabhu
Active Contributor
0 Kudos

Hi,

CALL METHOD cl_gui_frontend_services=>directory_exist

EXPORTING

directory = g_path -


> will this be the filepath directory (example: C:\ )

RECEIVING

result = x_directory -


> It is of type c (Contains 'X' or ' ').

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

wrong_parameter = 3

OTHERS = 4.

Based on the value of x_directory, u can write code as below.

Ex:

if x_directory = 'X'.

write:/ 'Directory exists'.

else.

write:/ 'Directory not exists'.

endif.

Former Member
0 Kudos

From above code I am not getting How will you validate the FILE TYPE ?

Former Member
0 Kudos

Hi

You can simply use this FM.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

EXPORTING

static = 'X'

mask = '*.XLS'

CHANGING

file_name = pa_fname

EXCEPTIONS

mask_too_long = 1

OTHERS = 2.

Here Pa_fname is the parameter field on the selection screen where f4 help will be provided and this fm will be called and you can select the path where you want to store.

Regards

Nitin.

0 Kudos

>

> Hi

>

> You can simply use this FM.

>

> CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

> EXPORTING

> static = 'X'

> mask = '*.XLS'

> CHANGING

> file_name = pa_fname

> EXCEPTIONS

> mask_too_long = 1

> OTHERS = 2.

>

>

> Here Pa_fname is the parameter field on the selection screen where f4 help will be provided and this fm will be called and you can select the path where you want to store.

>

> Regards

> Nitin.

But the person who will be running the program is also able to type whatever he feels like into the parameter and run the program! How do you know that he will click on the possible entries button (F4) and then choose the filename? Who is going to look over his shoulder and check??

0 Kudos

hi,

User can enter anything on the selection screen and it will accept that, but f4 help is given so as to make the selection of the path easy and user friendly and to store the file in .xls format by default which was the doubt.

Thanks,

Nitin.