Hello, (sorry for my bad english)
how do I know if a file is in use before erasing it?
Thank you
Control if file is used before deletefile()
- airsoftsoftwair
- Posts: 5433
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Control if file is used before deletefile()
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()...
Re: Control if file is used before deletefile()
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
Re: Control if file is used before deletefile()
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:
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
Re: Control if file is used before deletefile()
@JPV
Thank you very nice
Thank you very nice
- airsoftsoftwair
- Posts: 5433
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Control if file is used before deletefile()
Also note that this
can be radically shortened to that now:
Code: Select all
ExitOnError(False)
DeleteFile(file$)
code = GetLastError()
ExitOnError(True)
Code: Select all
code = ?DeleteFile(file$)
- airsoftsoftwair
- Posts: 5433
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Control if file is used before deletefile()
Yes, it works with all functions. See here.sinisrus wrote: ↑Tue May 28, 2019 11:24 pm @airsoftsoftwair
This ? work with other function or only DeleteFile() ?