How to check if file is busy/locked?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
squadjot
Posts: 4
Joined: 17 Dec 2009 13:14

How to check if file is busy/locked?

#1 Post by squadjot » 17 Dec 2009 13:25

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?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#2 Post by !k » 18 Dec 2009 03:59


DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 18 Dec 2009 23:47

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.

Post Reply