‎2009 May 08 7:32 AM
Hi experts,
I am writing a report, on the selection screen, I need to input the file path and then do the file upload.
My question is about how to check the file path I put is correct or not? If it is incorrect, I want to get a message and the cursor still in the field and don't jump to the next page.
How can I do like that?
Any one has any suggestion, please help me.
Thanks in advance.
Regards,
Chris Gu
‎2009 May 08 7:37 AM
Hello ,
Write the related code in AT SELECTION-SCREEN event
as below
If p_file Contains the reuired fiel type
for ex : .CSV , .TXT or .xls or .DAT
if u r condtion fails provide a Error messaage saying that File type is wrong ...
Regarding File path : If u have laredy the defined file path the u can use that
Regards
‎2009 May 08 7:37 AM
Hello ,
Write the related code in AT SELECTION-SCREEN event
as below
If p_file Contains the reuired fiel type
for ex : .CSV , .TXT or .xls or .DAT
if u r condtion fails provide a Error messaage saying that File type is wrong ...
Regarding File path : If u have laredy the defined file path the u can use that
Regards
‎2009 May 08 7:37 AM
hi,
See the below pseudo code.
PARAMETERS : p_upload TYPE rlgrap-filename.
AT SELECTION-SCREEN.
****validations on p_upload.
PERFORM f_validationson_upload.
*&---------------------------------------------------------------------*
*& f_validationson_upload
*&---------------------------------------------------------------------*
FORM f_validationson_upload .
DATA: lv_len TYPE i.
lv_len = STRLEN( p_upload ).
lv_len = lv_len - 4.
TRANSLATE p_dnload+lv_len TO UPPER CASE.
IF p_dnload+lv_len NE '.XLS'. "--> if file is not of type .'XLS' ,it will throw an error message
MESSAGE text-020 TYPE 'S'.
LEAVE TO SCREEN 1000.
ENDIF.
Thanks & REgards
‎2009 May 08 7:40 AM
Hi,
use the FM
CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
and method
CALL METHOD cl_gui_frontend_services=>directory_exist
CALL METHOD cl_gui_frontend_services=>file_exist
Pass related file in the above FM and methods and try to validate.
Regards,
jaya
‎2009 May 08 7:41 AM
Hi,
The FM whichyou are using for uploading the file will through the error message, then you can write the code set cursor on field and next populate the error message.
‎2009 May 08 7:42 AM
You can use FM: 'GUI_UPLOAD' to read the file into an internal table 'AT SELECTION-SCREEN' event. If filepath is wrong, SY-SUBRC at end of FM will not be 0. Check for that.
‎2009 May 08 7:43 AM
PARAMETERS :
p_path TYPE rlgrap-filename.
AT SELECTION-SCREEN ON p_path.
IF sscrfields-ucomm = 'ONLI'.
PERFORM validate_file_path USING p_path.
ENDIF. " IF SSCRFIELDS-UCOMM = 'ONLI'..
FORM validate_file_path USING pv_path LIKE p_path.
DATA: lw_path TYPE string,
lw_return TYPE abap_bool.
lw_path = pv_path.
IF sy-ucomm = 'ONLI' AND p_path IS INITIAL.
CLEAR sscrfields-ucomm.
MESSAGE e010(ad) WITH 'Fill in All the required fields'.
ELSE.
CALL METHOD cl_gui_frontend_services=>directory_exist
EXPORTING
directory = lw_path
RECEIVING
result = lw_return
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0 OR lw_return IS INITIAL.
CLEAR sscrfields-ucomm.
MESSAGE e010(ad) WITH 'Invalid Path'.
ENDIF. " IF SY-SUBRC <> 0 OR LW_RETURN.
ENDIF. " IF SY-UCOMM = 'ONLI' AND P_PA.
ENDFORM. " FORM VALIDATE_FILE_PATH USING.OR use: CALL METHOD cl_gui_frontend_services=>File_EXISTS
Regards,
Gurpreet
‎2009 May 08 7:45 AM
Hi,
You can use call method cl_gui_frontend_services=>file_exist to check if a file exists.
call method cl_gui_frontend_services=>file_exist
exporting
file = source_file_path
receiving
result = rc
exceptions
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
others = 4.
if sy-subrc <> 0.
raise cntl_error.
endif.
Regards.
‎2009 May 08 7:45 AM
Hi Chris,
do it this way: check my code after calling gui_upload what condition i am using.
parameters:
p_file type rlgrap-filename. " File name
*"--------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST EVENT
*"--------------------------------------------------------------------*
at selection-screen on value-request for p_file.
perform get_file_name.
*"--------------------------------------------------------------------*
* AT SELECTION-SCREEN EVENT
*"--------------------------------------------------------------------*
at selection-screen on p_file.
perform validate_upload_file.
*---------------------------------------------------------------------*
* Form GET_FILE_NAME
*---------------------------------------------------------------------*
form get_file_name.
call function 'F4_FILENAME'
exporting
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
importing
file_name = p_file.
endform. " GET_FILE_NAME
*---------------------------------------------------------------------*
* Form VALIDATE_UPLOAD_FILE
*---------------------------------------------------------------------*
form validate_upload_file.
data:
lw_file type string. " File Path
lw_file = p_file.
call function 'GUI_UPLOAD'
exporting
filename = lw_file
filetype = 'ASC'
has_field_separator = 'X'
dat_mode = 'X'
tables
data_tab = t_final_data.
IF sy-subrc ne 0 and t_final_data is initial. " here message if file path is wrong
Message 'File not found' type 'E'.
ELSEIF sy-subrc eq 0 and t_final_data is initial.
Message 'File empty' type 'E'.
ENDIF.
endform. " VALIDATE_UPLOAD_FILEWith luck,
Pritam.
Edited by: Pritam Ghosh on May 8, 2009 8:57 AM
‎2009 May 08 7:45 AM
May be this will help ful
Data : P_file(50) type c,
P_pathname(4) type c,
k(1024) type c.
constant : filepath(4) type c value '.txt'.
P_file = 'C:\Vendor.txt'.
k = strlen( p_file ).
k = k - 4.
p_pathname = p_file+k(4).
if filepath <> P_pathname.
Message 'Invalid path' TYPE 'E'.
Endif.
‎2009 May 08 7:46 AM
use FM F4_FILENAME in AT SELECTION-SCREEN ON VALUE-REQUEST event.
Regards.
Sarbajit.
‎2009 May 08 7:47 AM
Hi,
Use this method CL_GUI_FRONTEND_SERVICES->FILE_EXIST ti check the existence of file.
PARAMETERS: p_file TYPE char100.
DATA : g_file TYPE string.
DATA p_result TYPE c.
AT SELECTION-SCREEN ON p_file.
g_file = p_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = g_file
RECEIVING
result = p_result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF p_result EQ ' '.
MESSAGE e016(pn) WITH 'File Does not exist/Check the File path'.
ENDIF.
‎2009 May 08 8:08 AM
Hi,
You can use file_exist method of class cl_gui_frontend_services .
In the bellow code all your requirements are fulfilled .
TYPE-POOLS : abap.
DATA : w_result TYPE abap_bool,
w_file TYPE string.
PARAMETERS : p_file(30) TYPE c.
w_file = p_file.
CONDENSE w_file NO-GAPS.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = w_file
RECEIVING
result = w_result " Imports the results
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
IF w_result NE 'X'.
MESSAGE 'File path is not valid ! Enter a Valid path' TYPE 'I'.
EXIT. " To retan the selection-screen If the file path is not correct
ENDIF.
WRITE : 'Test'.Here if the w_result's value is 'X' then the file exists else does not exist.
So accordingly Message and Exit are used.
Regards
Pinaki
‎2009 May 08 9:22 AM
Hi,
I have one doubt, I have input a correct file path and the file exists in the local server,
when I use the directory_exist method, result is still empty. Why?
Regards,
Chris Gu
Edited by: Gu Chris on May 8, 2009 10:28 AM
‎2009 May 08 9:28 AM
Solved.
Used the file_exist method, I can get the result = 'X' if I enter the existed file path.
Thanks all.
Regards,
Chris Gu