BBC BASIC for Windows
« Error Handling When Deleting File »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:04pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Error Handling When Deleting File  (Read 602 times)
Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Error Handling When Deleting File
« Thread started on: Jul 27th, 2013, 08:09am »

Hi,

Having trouble working out how to trap and use an 'Access denied' error during a delete file routine.

Code:
      DEF FN_delfile(file$)
      LOCAL dir%, sh%
      ON ERROR LOCAL IF ERR = 189 THEN = FALSE
      DIM dir% LOCAL 317
      SYS "FindFirstFile", file$, dir% TO sh%
      IF sh% <> -1 THEN SYS "FindClose", sh% ELSE = FALSE
      OSCLI "DELETE """ + file$ + """"
      = TRUE 


This still errors on the OSCLI line without producing an error report. What am I doing wrong? I've checked the help and it doesn't seem to allow error handling within the subroutine itself - or am I, as usual, missing something?

Matt
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Error Handling When Deleting File
« Reply #1 on: Jul 27th, 2013, 09:28am »

on Jul 27th, 2013, 08:09am, Matt wrote:
Having trouble working out how to trap and use an 'Access denied' error during a delete file routine.

I can't reproduce that. What is causing the 'Access denied' error? I've tried using your FN_delfile() routine with a file that doesn't exist, and a file set to read-only, and in both cases it returns zero without any error being reported.

Incidentally there's an easier way of testing whether a file exists than using FindFirstFile, just attempt to open it using OPENIN:

Code:
      F% = OPENIN(file$)
      IF F% CLOSE #F% : PRINT "File exists" 

I should also point out that in fact there's no point first testing whether the file exists, because there's a finite chance that it did exist when FindFirstFile is called, but by the time the OSCLI statement is executed it no longer exists (because an intervening task switch ran a process that deleted the file).

So because of that, admittedly small, chance you might just as well not bother to test whether the file exists and let the delete fail:

Code:
      DEF FN_delfile(file$)
      ON ERROR LOCAL = FALSE
      OSCLI "DELETE """ + file$ + """"
      = TRUE 

One final suggestion is that you can avoid an error occurring altogether by calling the DeleteFile API:

Code:
      DEF FN_delfile(file$)
      LOCAL R%
      SYS "DeleteFile", file$ TO R%
      = (R% <> 0) 

Richard.
« Last Edit: Jul 27th, 2013, 09:30am by admin » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls