-2

I basically want to make a watch service (or something like it) that checks if a file has been closed and instantly remove that file if it did close(finished executing).

How I can achieve this? please give me a cmd commands or some code(i prefer Java).

Eboubaker
  • 618
  • 7
  • 15
  • I feel like it would be much simpler to just wrap the input stream and override the close method to also invoke delete. – vandench Jul 12 '19 at 13:53
  • @vandench it is not about files that are open by the current program. it can be open by another program. – Eboubaker Jul 12 '19 at 14:20

2 Answers2

1

Ok, this should not be hard to do, if you google a bit you find a Java-File Method called file.canWrite() which basically returns if a file is locked by an other program or so.

So codewise what you could do is something like this.

boolean isDeleted = false;
File f = new File (// Put your file here);
while (!isDeleted) {
    if (f.canWrite()) {
        f.delete();
        isDeleted = true;
    } else {
        try {
            Thread.sleep(10); // Throws Exception you need to catch somewhere...
        } catch (Exception e) {}
    }
}

This code you need to include into some Java-Program. I added a simple Thread.sleep(10) that your PC does not have to check aaaaaalllllllll the time. See Check if a file is locked in Java

Other possibility would be trying to rename the file with file.renameTo("some_path.txt"); as this method also returns a boolean whether it was successfull! Just note that you then need to update the file again before removing it.

Last possibility I see is pretty similar to the second one. You try to delete the file by calling file.delete(); If the file still exists you know it was not successful and loop because of that.

Cedric
  • 408
  • 1
  • 4
  • 18
  • Are you sure that `File.canWrite()` checks whether the file is locked by another process? My understanding is that it will not check that, see e.g. https://stackoverflow.com/questions/1500174/check-if-a-file-is-locked-in-java/13706972 . I have not tested it myself – Rich Jul 12 '19 at 13:58
  • I will try to implement that in a thread., good idea ..if it works – Eboubaker Jul 12 '19 at 14:03
  • @Cedced_Bro this didn't work it tried to delete it while it is still running. – Eboubaker Jul 12 '19 at 14:09
  • @Cedced_Bro actually your idea made me think of another one, I will do the same checking but instead of calling .canWrite() I will call .delete() then I check if it still exists if it does than loop again, slower but it will do the job. – Eboubaker Jul 12 '19 at 14:17
0

I assume you mean when the file is not open in another program, and you cannot make changes to that other program? (If you are talking about your own program opening the file, this is much easier.)

On Windows, it is not very easy to tell which program has a file open. Take a look at https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows for some options. I like the handle tool for this, but it has to run as Administrator, which may be a problem. You can try renaming or writing to the file, as suggested at Check if a file is locked in Java

Once you have a script that determines whether the file is open to your satisfaction, it should be fairly straightforward to write a script which loops while testing if the file is open and then deletes file.

Rich
  • 15,048
  • 2
  • 66
  • 119