[solved] check if a file exists on Harddisk?

Hello!
I’m on NaturalOne Ajax 8.2 with the following question:
In a main program, I have to check if a file exists on hard disk. I’ve tried:

 
REQUEST DOCUMENT FROM #fileonhdd RESPONSE #FileExistRPCode
 

(#fileonhdd is filled with C:/TEMP/TEST.TXT)

But on this way I get an error: " wrong protocol…" (maybe just only for http/https?)

How can I check if a file exists on Harddisk?

Thank you ver much,
Matthias

Hi,

we wrote an Natural-Modul which checks this with unix utilities:

Directory-Check:
COMPRESS ‘"’ P-DIR ‘"’ INTO OS-COMMAND LEAVING NO
COMPRESS ‘test -d’ OS-COMMAND INTO OS-COMMAND
*
CALLNAT ‘USR1052N’ USR1052L
P-RC := RESPONSE

File-Check:
COMPRESS ‘"’ P-DIR ‘"’ INTO OS-COMMAND LEAVING NO
COMPRESS ‘test -f’ OS-COMMAND INTO OS-COMMAND
*
CALLNAT ‘USR1052N’ USR1052L
P-RC := RESPONSE

P-RC = 0 => OK
P-RC > 0 => not OK

Maybe there is an Windows command …

best regards
Markus Wessjohann

If you define C:/TEMP/TEST.TXT as a work file, you could do a READ WORK FILE for the first record.

@ Steve Robinson
yes, but in this case I whould get an ERROR if the file does not exist, and I must not go into an ERROR :?

thank you very much,
matthias

Use an ON ERROR clause.

I did it with DEFINE/READ WORK FILE. It works, but just only once (ON ERROR). When I come a second time to the
READ WORKFILE statement and ERROR-NR 1599 in my program, this ON ERROR-Block is ignored.

Does my Program “forget” this ON ERROR-BLOCK?
Can you explane me why?

ON ERROR
IF *ERROR-NR = 1599
PROCESS PAGE UPDATE FULL AND SEND EVENT 'nat:page.message'
  WITH PARAMETERS
   NAME 'type' VALUE 'E'
   NAME 'short' VALUE 'No File found'
 END-PARAMETERS
END-IF 
END-ERROR

Thank you very much,
Matthias

ON ERROR is a non procedural clause in Natural. More importantly, it interrupts the execution of a program.

With the exception of a record-held error (3145), which permits execution to continue via RETRY, your program ends after the ON ERROR clause ends.

Thus, for anything to happen again, you must have logic in the ON ERROR clause that continues execution of your “system”. For example, you might FETCH a driver program to your existing program. You could use the stack, or *COM, to “tell” the driver what happened.

steve

Build a new modul CHECKFILE with the ON ERROR cause:

like this:
P-FILEEXITS := 0
DEFINE WORK FILE 32 P-FILENAME …
CLOSE WORK FILE 32

ON ERROR
P-FILEEXITS := 1
ESCAPE MODULE
END-ERROR

Everytime you want to read a file, you can call the modul, to check if it existing.

The problem is you must be sure, that no other modul is using workfilenr 32 :frowning:

best regards
Markus Wessjohann

did it like you’ve written, it works! :smiley:
thank you very much!
Matthias