Control if file is used before deletefile()

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

Control if file is used before deletefile()

Post by sinisrus » Mon May 27, 2019 4:44 pm

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: 3206
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Control if file is used before deletefile()

Post by airsoftsoftwair » Mon May 27, 2019 5:01 pm

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: 263
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus » Mon May 27, 2019 11:39 pm

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: 281
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Control if file is used before deletefile()

Post by jPV » Tue May 28, 2019 10:09 am

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: 263
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus » Tue May 28, 2019 11:58 am

@JPV

Thank you very nice :-)

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

Re: Control if file is used before deletefile()

Post by airsoftsoftwair » Tue May 28, 2019 5:49 pm

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: 263
Joined: Tue Apr 21, 2015 5:43 pm

Re: Control if file is used before deletefile()

Post by sinisrus » Tue May 28, 2019 11:24 pm

@airsoftsoftwair

Ohh good :-)

This ? work with other function or only deletefile() ?

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

Re: Control if file is used before deletefile()

Post by airsoftsoftwair » Wed May 29, 2019 9:39 pm

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