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

Real time file exist check...

Former Member
0 Likes
1,843

Hello all,

Would like to ask, is there any way in ABAP to create a real-time file(S) exist check let's say in local PC drive/directory?

Thanks in advance!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,772

Use the class cl_gui_frontend_services. Based on the RESULT parameter you can check if file exists or not.

TYPE-POOLS : abap.

DATA : l_result TYPE abap_bool.

CALL METHOD cl_gui_frontend_services=>file_exist
  EXPORTING
    file                 = 'C:\boot.ini'
  RECEIVING
    result               = l_result
  EXCEPTIONS
    cntl_error           = 1
    error_no_gui         = 2
    wrong_parameter      = 3
    not_supported_by_gui = 4
    OTHERS               = 5.

IF l_result = 'X'.
  WRITE : 'File Exists'.
ELSE.  
  WRITE : 'File does not Exists'.
ENDIF.

Regards

Rajvansh

Edited by: Rajvansh Ravi on Aug 20, 2008 8:31 AM

Hello all,

Would like to ask, is there any way in ABAP to create a real-time file(S) exist check let's say in local PC drive/directory?

Thanks in advance!

11 REPLIES 11
Read only

Former Member
0 Likes
1,772

Hi,

1. TMP_GUI_GET_FILE_EXIST

We can use the above Fm.

2. If the file does not exist, it will the parameter FILESIZE = -1.

If the file exists, it will return the filesize, and also

if it is a folder or not.

regards,

amit m.

Read only

Former Member
0 Likes
1,773

Use the class cl_gui_frontend_services. Based on the RESULT parameter you can check if file exists or not.

TYPE-POOLS : abap.

DATA : l_result TYPE abap_bool.

CALL METHOD cl_gui_frontend_services=>file_exist
  EXPORTING
    file                 = 'C:\boot.ini'
  RECEIVING
    result               = l_result
  EXCEPTIONS
    cntl_error           = 1
    error_no_gui         = 2
    wrong_parameter      = 3
    not_supported_by_gui = 4
    OTHERS               = 5.

IF l_result = 'X'.
  WRITE : 'File Exists'.
ELSE.  
  WRITE : 'File does not Exists'.
ENDIF.

Regards

Rajvansh

Edited by: Rajvansh Ravi on Aug 20, 2008 8:31 AM

Read only

0 Likes
1,772

Hi all,

Thanks for all the inputs. BUt what I was asking is real time checking on the local PC drive/directory.

The above suggestions are only can be triggered once. I heard someone...in JAVA, we can use something like LISTENER.

Is this available in ABAP?

Read only

0 Likes
1,772

Hi,

1. If you are talking about real time,

then in that case the z program will continuously run (in foreground ofcourse).

It will continually check (eg. every 1 second) for the existence of file - using the FM or class method.

2. In such continous checking, we have to use the timer concept.

3. Below is a program which continuosly

checks for the existence of file

D:\abc.txt

(u can change that file in the program)

If the file is not found (continously) it keeps on

displaying 'File NOT Found'

if found it displays 'File FOUND'

4. Just copy paste to get a taste of it.

5.




REPORT abc NO STANDARD PAGE HEADING.

DATA : wa(72) TYPE c.


*-------------------------------------

CLASS my DEFINITION.

PUBLIC SECTION.

METHODS : run_handler FOR EVENT finished OF cl_gui_timer.

ENDCLASS. "my DEFINITION

DATA timer TYPE REF TO cl_gui_timer.
DATA myh TYPE REF TO my.


*---------------------------------------------------------------------*
* CLASS my IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS my IMPLEMENTATION.
METHOD run_handler.
CALL METHOD timer->run.
PERFORM mylist.
ENDMETHOD. "run_handler

ENDCLASS. "my IMPLEMENTATION

*-------------------------------------


*-------------- Init

INITIALIZATION.

CREATE OBJECT timer.
CREATE OBJECT myh.
SET HANDLER myh->run_handler FOR ALL INSTANCES.


* PARAMETERS : a TYPE c.

*----------- End of selection

START-OF-SELECTION.

WRITE sy-uzeit TO wa.
WRITE wa.
timer->interval = '0.5'.
CALL METHOD timer->run.



*---------------------------------
FORM mylist.

*--------------------------
*WRITE sy-uzeit TO wa.

data : filesize type i.

CALL FUNCTION 'TMP_GUI_GET_FILE_EXIST'
  EXPORTING
    FNAME                = 'D:\abc.txt'
 IMPORTING
*   EXIST                =
*   ISDIR                =
   FILESIZE             = filesize
 EXCEPTIONS
   FILEINFO_ERROR       = 1
   OTHERS               = 2
          .

if filesize  = -1.
wa = 'File NOT Found'.
else.
wa = 'File FOUND !!!'.
endif.
MODIFY LINE 1 LINE VALUE FROM wa.

ENDFORM. "MYLIST


regards,

amit m.

Read only

0 Likes
1,772

Hi amit m.,

Yes, you have answer what I want accurately.

Really thanks for your sample code. It works at my side :).

I have a concern, if I write a code to check in file exist every 0.5 second or 1 second.

Will this real time checking affects the whole SAP performance? Will it slow down the application?

Thanks.

Read only

0 Likes
1,772

How could I stop the real- time checking if "File Found"?

Thanks.

Read only

0 Likes
1,772

How could I stop the real- time checking if "File Found"?

Check Sy-subrc value.

Read only

0 Likes
1,772

Hi

Data : fullpath type string.

data : result type c.

call method cl_gui_frontend_services=>file_exist

exporting

file = fullpath

receiving

result = result.

if result is not initial.

Write : 'File Exit' .

else.

write 'File Dose Not Exit'.

endif.

Thanks,

Durai.V

Read only

0 Likes
1,772

Thanks. But where should I include this CHECK at? which line?

I have tried few places...but not successful.

Read only

0 Likes
1,772

Hi again,

1. Don't worry about load on server 0.5 seconds or 1 seconds

is no big deal for server.

2. This is the slightly modified code

for stopping if the file is found.

Just copy paste.

(There is only one line which I added for stopping if file found)



REPORT abc NO STANDARD PAGE HEADING.

DATA : wa(72) TYPE c.


*-------------------------------------

CLASS my DEFINITION.

PUBLIC SECTION.

METHODS : run_handler FOR EVENT finished OF cl_gui_timer.

ENDCLASS. "my DEFINITION

DATA timer TYPE REF TO cl_gui_timer.
DATA myh TYPE REF TO my.


*---------------------------------------------------------------------*
* CLASS my IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS my IMPLEMENTATION.
METHOD run_handler.
CALL METHOD timer->run.
PERFORM mylist.
ENDMETHOD. "run_handler

ENDCLASS. "my IMPLEMENTATION

*-------------------------------------


*-------------- Init

INITIALIZATION.

CREATE OBJECT timer.
CREATE OBJECT myh.
SET HANDLER myh->run_handler FOR ALL INSTANCES.


* PARAMETERS : a TYPE c.

*----------- End of selection

START-OF-SELECTION.

WRITE sy-uzeit TO wa.
WRITE wa.
timer->interval = '0.5'.
CALL METHOD timer->run.



*---------------------------------
FORM mylist.

*--------------------------
*WRITE sy-uzeit TO wa.

data : filesize type i.

CALL FUNCTION 'TMP_GUI_GET_FILE_EXIST'
  EXPORTING
    FNAME                = 'D:\abc.txt'
 IMPORTING
*   EXIST                =
*   ISDIR                =
   FILESIZE             = filesize
 EXCEPTIONS
   FILEINFO_ERROR       = 1
   OTHERS               = 2
          .

if filesize  = -1.
wa = 'File NOT Found'.
else.
wa = 'File FOUND !!!'.
*---------- Stop timer if file found
timer->interval = '0'.
endif.

MODIFY LINE 1 LINE VALUE FROM wa.
ENDFORM. "MYLIST

regards,

amit m.

Read only

0 Likes
1,772

Hi Amit,

Excellent work!

You really help me alot on this.

Thank you so much.