‎2014 Mar 11 4:23 PM
Hi Gurus,
I would like to trigger an ABAP program as and when a file is dropped in a drive. Please let me know what is the best way to do this.
Also please let me know how to handle in the program if 2 or more files are dropped at a time.
Appreciate it. thanks.
‎2014 Mar 11 6:37 PM
Hello,
You can create a Z program and schedule a job for the time what you need.
Sample code:
"......... definitions .......
START-OF-SELECTION.
PERFORM read_files_in_dir_on_server USING p_wfile
CHANGING gt_files.
LOOP AT gt_files INTO gv_filepath.
gv_progress = sy-tabix / lines( gt_files ).
PERFORM read_server_file USING gv_filepath
CHANGING gt_file_content
gv_filelenght.
"do what you need here!
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = gv_progress
text = 'Processing the files'(003).
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form READ_FILES_IN_DIR_ON_SERVER
*&---------------------------------------------------------------------*
* Read all files in a directory (server)
*----------------------------------------------------------------------*
* -->IV_DIR Directory
* <--CT_FILES Files in Directory
*----------------------------------------------------------------------*
FORM read_files_in_dir_on_server USING iv_dir TYPE string
CHANGING ct_files LIKE gt_files.
DATA: lv_dir_name TYPE ocs_file-name,
lt_filelist TYPE STANDARD TABLE OF ocs_file,
ls_filelist LIKE LINE OF lt_filelist,
ls_file LIKE LINE OF ct_files,
lv_offset TYPE i,
lv_dummy TYPE i,
lv_error TYPE boolean.
lv_dir_name = iv_dir.
CALL FUNCTION 'OCS_GET_FILE_INFO'
EXPORTING
dir_name = lv_dir_name
TABLES
dir_list = lt_filelist
EXCEPTIONS
no_authority = 1
activity_unknown = 2
not_a_directory = 3
no_media_in_drive = 4
too_many_errors = 5
too_many_files = 6
bracket_error_in_filename = 7
no_such_parameter = 8
OTHERS = 9.
IF sy-subrc <> 0.
MESSAGE 'Error on directory selected' TYPE 'E'.
ENDIF.
LOOP AT lt_filelist INTO ls_filelist.
lv_error = abap_false.
"check a specific prefix
IF ls_filelist-name(4) NE 'out_'.
lv_error = abap_true.
ENDIF.
"check a specific extension
FIND '.xml' IN ls_filelist-name MATCH OFFSET lv_offset.
IF sy-subrc IS NOT INITIAL.
lv_error = abap_true.
ENDIF.
"needs to end with a number (not chars allowed)
lv_offset = lv_offset - 1.
TRY .
lv_dummy = ls_filelist-name+lv_offset(1).
CATCH cx_root.
lv_error = abap_true.
ENDTRY.
"check if the dir is complete filled (end with \ )
lv_offset = strlen( iv_dir ) - 1.
IF iv_dir+lv_offset EQ '\'.
CONCATENATE iv_dir
ls_filelist-name
INTO ls_file.
ELSE.
CONCATENATE iv_dir '\'
ls_filelist-name
INTO ls_file.
ENDIF.
IF lv_error NE abap_true.
APPEND ls_file TO ct_files.
CLEAR ls_file.
ENDIF.
ENDLOOP.
ENDFORM. " READ_FILES_IN_DIR_ON_SERVER
*&---------------------------------------------------------------------*
*& Form READ_SERVER_FILE
*&---------------------------------------------------------------------*
* Read server file
*----------------------------------------------------------------------*
* -->IV_FILEPATH text
* <--CT_FILE_CONTENT text
* <--CV_FILELENGHT text
*----------------------------------------------------------------------*
FORM read_server_file USING iv_filepath TYPE string
CHANGING ct_file_content TYPE table_of_strings
cv_filelenght TYPE i.
DATA: lv_content LIKE LINE OF ct_file_content,
lv_msg TYPE string.
REFRESH ct_file_content.
CLEAR cv_filelenght.
OPEN DATASET iv_filepath FOR INPUT
IN TEXT MODE
ENCODING UTF-8
MESSAGE lv_msg.
DO.
READ DATASET iv_filepath INTO lv_content.
IF sy-subrc IS NOT INITIAL.
EXIT.
ENDIF.
APPEND lv_content TO ct_file_content.
gv_filelenght = gv_filelenght + strlen( lv_content ).
ENDDO.
ENDFORM. " READ_SERVER_FILE
‎2014 Mar 11 4:25 PM
Hi,
Can you please be more specific? Are you dropping the file on AL11 or to a folder in your workstation/laptop?
Regards,
Rahul MB
‎2014 Mar 11 5:49 PM
Hello,
May be you can try like below.
Create a Z report like below and schedule it in background with the frequency of say 5mins.
--> Code to check the file, either in application server or presentation server.
--> If the file exists, run your program with SUBMIT statement.
Regards,
Thanga
‎2014 Mar 11 6:06 PM
Thanks Thanga. Can you please give me code to pull all files from the folder into z report that you are talking about, then pass one by one to submit statement.
‎2014 Mar 11 6:27 PM
Are the files dropped in application server or presentation server?
‎2014 Mar 11 6:29 PM
‎2014 Mar 11 6:51 PM
Hello,
Check the below code.
Create a Z report like this and schedule it in background for a specified frequency.
DATA: C_lv_exists TYPE XFLAG.
PARAMETERS: p_file LIKE DXFILE-FILENAME.
call method cl_gui_frontend_services=>file_exist
exporting
file = P_file
receiving
result = C_lv_exists
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
others = 4.
OR
CALL FUNCTION 'DX_FILE_EXISTENCE_CHECK'
EXPORTING
filename = P_file
pc = 'X'
* SERVER =
IMPORTING
FILE_EXISTS = C_lv_exists
EXCEPTIONS
RFC_ERROR = 1
FRONTEND_ERROR = 2
NO_AUTHORITY = 3
OTHERS = 4.
IF C_lv_exists = 'X'.
SUBMIT Z* program which you want to trigger.
ENDIF.
Regards,
Thanga
‎2014 Mar 11 6:37 PM
Hello,
You can create a Z program and schedule a job for the time what you need.
Sample code:
"......... definitions .......
START-OF-SELECTION.
PERFORM read_files_in_dir_on_server USING p_wfile
CHANGING gt_files.
LOOP AT gt_files INTO gv_filepath.
gv_progress = sy-tabix / lines( gt_files ).
PERFORM read_server_file USING gv_filepath
CHANGING gt_file_content
gv_filelenght.
"do what you need here!
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = gv_progress
text = 'Processing the files'(003).
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form READ_FILES_IN_DIR_ON_SERVER
*&---------------------------------------------------------------------*
* Read all files in a directory (server)
*----------------------------------------------------------------------*
* -->IV_DIR Directory
* <--CT_FILES Files in Directory
*----------------------------------------------------------------------*
FORM read_files_in_dir_on_server USING iv_dir TYPE string
CHANGING ct_files LIKE gt_files.
DATA: lv_dir_name TYPE ocs_file-name,
lt_filelist TYPE STANDARD TABLE OF ocs_file,
ls_filelist LIKE LINE OF lt_filelist,
ls_file LIKE LINE OF ct_files,
lv_offset TYPE i,
lv_dummy TYPE i,
lv_error TYPE boolean.
lv_dir_name = iv_dir.
CALL FUNCTION 'OCS_GET_FILE_INFO'
EXPORTING
dir_name = lv_dir_name
TABLES
dir_list = lt_filelist
EXCEPTIONS
no_authority = 1
activity_unknown = 2
not_a_directory = 3
no_media_in_drive = 4
too_many_errors = 5
too_many_files = 6
bracket_error_in_filename = 7
no_such_parameter = 8
OTHERS = 9.
IF sy-subrc <> 0.
MESSAGE 'Error on directory selected' TYPE 'E'.
ENDIF.
LOOP AT lt_filelist INTO ls_filelist.
lv_error = abap_false.
"check a specific prefix
IF ls_filelist-name(4) NE 'out_'.
lv_error = abap_true.
ENDIF.
"check a specific extension
FIND '.xml' IN ls_filelist-name MATCH OFFSET lv_offset.
IF sy-subrc IS NOT INITIAL.
lv_error = abap_true.
ENDIF.
"needs to end with a number (not chars allowed)
lv_offset = lv_offset - 1.
TRY .
lv_dummy = ls_filelist-name+lv_offset(1).
CATCH cx_root.
lv_error = abap_true.
ENDTRY.
"check if the dir is complete filled (end with \ )
lv_offset = strlen( iv_dir ) - 1.
IF iv_dir+lv_offset EQ '\'.
CONCATENATE iv_dir
ls_filelist-name
INTO ls_file.
ELSE.
CONCATENATE iv_dir '\'
ls_filelist-name
INTO ls_file.
ENDIF.
IF lv_error NE abap_true.
APPEND ls_file TO ct_files.
CLEAR ls_file.
ENDIF.
ENDLOOP.
ENDFORM. " READ_FILES_IN_DIR_ON_SERVER
*&---------------------------------------------------------------------*
*& Form READ_SERVER_FILE
*&---------------------------------------------------------------------*
* Read server file
*----------------------------------------------------------------------*
* -->IV_FILEPATH text
* <--CT_FILE_CONTENT text
* <--CV_FILELENGHT text
*----------------------------------------------------------------------*
FORM read_server_file USING iv_filepath TYPE string
CHANGING ct_file_content TYPE table_of_strings
cv_filelenght TYPE i.
DATA: lv_content LIKE LINE OF ct_file_content,
lv_msg TYPE string.
REFRESH ct_file_content.
CLEAR cv_filelenght.
OPEN DATASET iv_filepath FOR INPUT
IN TEXT MODE
ENCODING UTF-8
MESSAGE lv_msg.
DO.
READ DATASET iv_filepath INTO lv_content.
IF sy-subrc IS NOT INITIAL.
EXIT.
ENDIF.
APPEND lv_content TO ct_file_content.
gv_filelenght = gv_filelenght + strlen( lv_content ).
ENDDO.
ENDFORM. " READ_SERVER_FILE