Control if file is used before deletefile()

Discuss any general programming issues here
Post Reply
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Control if file is used before deletefile()

Post by sinisrus »

Hello, (sorry for my bad english)

how do I know if a file is in use before erasing it?

Thank you
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Control if file is used before deletefile()

Post by airsoftsoftwair »

That's not possible and doesn't make sense in a multitasking environment. Even if there was a function called IsFileInUse() it wouldn't make sense because some other process could lock the file between your call to IsFileInUse() and the call to DeleteFile()...
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus »

For example I open picture with multiview or picshow and with my program hollywood I want to erase this picture my program will return an error and quit ... it's not very clean
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Control if file is used before deletefile()

Post by jPV »

You can disable the internal error handler before trying to delete a file, check yourself if it failed, and enable the error handler again after that.

If you're using it often, maybe it's better to write an own delete function, like this:

Code: Select all

Function p_DeleteFile(file$)
    Local code
    ExitOnError(False)
    DeleteFile(file$)
    code = GetLastError()
    ExitOnError(True)
    If code
        DebugPrint("Error while deleting \"" .. file$ .. "\":", GetErrorName(code))
    Else
        DebugPrint("\"" .. file$ .. "\" deleted.")
    EndIf
    Return(code)
EndFunction

StringToFile("bla", "testfile")          ; Create a new file
OpenFile(1, "testfile", #MODE_READWRITE) ; Let's keep the file open
p_DeleteFile("testfile")                 ; Deleting fails, but the script continues
CloseFile(1)                             ; Let's close the file
p_DeleteFile("testfile")                 ; Deleting works now
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus »

@JPV

Thank you very nice :-)
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Control if file is used before deletefile()

Post by airsoftsoftwair »

Also note that this

Code: Select all

ExitOnError(False)
DeleteFile(file$)
code = GetLastError()
ExitOnError(True)
can be radically shortened to that now:

Code: Select all

code = ?DeleteFile(file$)
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus »

@airsoftsoftwair

Ohh good :-)

This ? work with other function or only DeleteFile() ?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Control if file is used before deletefile()

Post by airsoftsoftwair »

sinisrus wrote: Tue May 28, 2019 11:24 pm @airsoftsoftwair
This ? work with other function or only DeleteFile() ?
Yes, it works with all functions. See here.
Post Reply