B4J Question Check if file is in use

Chianca

Member
Licensed User
Hello!

How can I check if a centain file is in use by another process?

I'm trying to delete a file, and sometimes getting an error because it is used by another proccess.

B4X:
File.Delete(File.DirApp & "/imp",FileName)
 

Chianca

Member
Licensed User
Hello!

I have solved the problem. I'm using FileWatcher Lib, and now I'm testing if file is readeble or writable before manipulate it.

Thank you!
 

inakigarm

Well-Known Member
Licensed User
Hello!

How can I check if a centain file is in use by another process?

I'm trying to delete a file, and sometimes getting an error because it is used by another proccess.

B4X:
File.Delete(File.DirApp & "/imp",FileName)
Maybe
B4X:
Try Catch End Try
would do the job? (Logging LastException message)
 

Harris

Expert
Licensed User
Hello!

How can I check if a certain file is in use by another process?

I'm trying to delete a file, and sometimes getting an error because it is used by another process.

B4X:
File.Delete(File.DirApp & "/imp",FileName)
If File.Delete(dir,filename) then...

should return whether file was deleted or not... What error do you see?

I sometimes run into this issue and just wait for the "next" cleanup cycle to get rid of unwanted files - in case some process has a handle on it (locking it).
This is usually in a temp dir when files are uploaded, download or associated with some DB record and a user deletes it.
At times, I find the OS has not released a handle to it - for whatever reason and it remains kicking around. A server reboot releases the lock - and then it can be removed, however... now we need to code more logic to remove files that couldn't be after restoring the OS.... six of one / half a dozen of the other...

Try Catch End Try
So in the Catch, log to another source table to try again at some other date?
OR - Try 10 times and give up? Not an issue with small files but a 2gig hogs space... and we want it to go - eventually.

I'm using FileWatcher Lib, and now I'm testing if file is readeble or writable before manipulate it.
So, if it is still locked - what do you do?

An issue indeed...
 

Chianca

Member
Licensed User
When Creation_Detected event of my FileWatcher is fired, I'm reading the file, open browser with it's content, and than, delete the file.

Yes, I can avoid this error by using Try / Catch, but I think if I test its state (is in use?), i would delete it with no problems.
 

ThRuST

Well-Known Member
Licensed User
@Chianca You might want to use a timer to check if the folder or file exists or not. You can use Try catch to play it safe. Automation is the future :)
 

ThRuST

Well-Known Member
Licensed User
@Chianca You might want to edit the title and add [SOLVED]. You do that by selecting from the top right menu. Edit title that is.
 
Top