B4J Question Check if file is in use

Discussion in 'B4J Questions' started by Chianca, Jul 3, 2019.

  1. Chianca

    Chianca Member Licensed User


    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.

    File.Delete(File.DirApp & "/imp",FileName)
  2. Chianca

    Chianca Member Licensed User


    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!
  3. inakigarm

    inakigarm Well-Known Member Licensed User

    Try Catch End Try
    would do the job? (Logging LastException message)
    ThRuST likes this.
  4. Harris

    Harris Well-Known Member Licensed User

    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...

    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.

    So, if it is still locked - what do you do?

    An issue indeed...
    ThRuST and Chianca like this.
  5. Chianca

    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.
  6. ThRuST

    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 :)
    Chianca likes this.
  7. ThRuST

    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.
    Harris likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice