Page 1 of 1

How to check if file is busy/locked?

Posted: 17 Dec 2009 13:25
by squadjot
Hi i'm having trouble finding a clean solution that checks if a file is busy/locked.

I know one way would be try to rename the file, if it suceeds, the file is not locked and you rename the file back to it's original filename.
I think it's a bit nasty, alltho it works in most cases, so i would like to have something more clean!

I had anohter solution, using a compiled 3rd party exe, called fsum.exe (SlavaSoft), it creates checksums for files, and the good thing was that this tool returns "DENIED" if the file is busy/locked. The "not so good" was that, because of the way fsum.exe works, large files could take up to seconds to read.

So, does anyone found a clean way to check if a file is busy?

Posted: 18 Dec 2009 03:59
by !k

Posted: 18 Dec 2009 23:47
by DosItHelp
There are different states a file can be in:
1. can be renamed but can't be delete.
2. can't be renamed and can't be deleted.

To check for the second case you could try to move the file onto itself.
move file.txt file.txt 2>NUL >NUL||echo.locked

If calling "move /-y file.txt file.txt" (with /-y option) when the file is actually not in use then it doesn't seem to ask for the "Overwrite? (Yes/No/All):" question, which suggests that it leaves the file alone because it is already on the target spot leaving us with pure "is file movable" check.

Hope this helps somewhat.