Page 1 of 2

Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 01:01
by sambasiva
Hi,

Is there any direct windows command to find if a file is in use or not ? (like lsof command on linux)

Is there any other batch script or commands possible to know if a file is in use in windows ?

Thanks
Sambasiva

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 02:17
by foxidrive
This is pasted from a prior thread.

It lists the files in a folder and waits until it is free/writeable.


Code: Select all

:: file locked/in use or writeable/free
@echo off
cd /d "c:\path\to\folder"

for %%a in (*.*) do call :next "%%a"
echo done
pause
goto :eof

:next
set "filename=%~1"
:loop
2>nul (call;>>"%filename%") && (
  echo "%filename%" is free! & goto :done
) || (
  echo "%filename%" is in use or is read only!
)
timeout /t 60 /nobreak
goto :loop
:done

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 03:04
by sambasiva
Thanks for the reply.

The statemnt call;>>"%filename%" tries to appending something to the file.

Any idea what does "call;" means ?

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 03:32
by aGerman
CALL; doesn't output anyting but it resets the errorlevel to zero. The pipe >> tries to open the file though. If this was successful (errorlevel 0) the command line next to the && operator will be executed. In case it failed (errorlevel >0) the command next to the || Operator will be executed.
The assumption is that the file was locked for writing in case it was opened with another program. That's depending on the program though. It could also fail. For example a text file opend with notepad is not locked and can't be detected this way.

Regards
aGerman

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 03:53
by foxidrive
aGerman wrote:The assumption is that the file was locked for writing in case it was opened with another program. That's depending on the program though. It could also fail. For example a text file opend with notepad is not locked and can't be detected this way.


You're quite right about that. The file has to be opened for exclusive access.

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 04:08
by aGerman
"locked for writing" was just loosely speaking. We could do some tests with CreateFile and its share modes.

Regards
aGerman

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 07:11
by Squashman
foxidrive wrote:This is pasted from a prior thread.

It lists the files in a folder and waits until it is free/writeable.


Code: Select all

:: file locked/in use or writeable/free
@echo off
cd /d "c:\path\to\folder"

for %%a in (*.*) do call :next "%%a"
echo done
pause
goto :eof

:next
set "filename=%~1"
:loop
2>nul (call;>>"%filename%") && (
  echo "%filename%" is free! & goto :done
) || (
  echo "%filename%" is in use or is read only!
)
timeout /t 60 /nobreak
goto :loop
:done

Interesting. I have never seen it with the CALL used. So I had to check the SO post of Dave's to double check and sure enough he updated his code last month to use the CALL.
http://stackoverflow.com/questions/1051 ... 9#10520609

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 08:54
by miskox
This might work:

Code: Select all

move /y a.a a.a >nul 2>nul||echo File locked.


Saso

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 09:00
by foxidrive
That works in my test with c:\swapfile.sys

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 09:13
by Squashman
Didn't work for a text file I had opened with Notepad. But I think we have already established that Notepad will not lock the file.

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 09:34
by foxidrive
swapfile.sys is opened for exlcusive use so should be a good test.

I guess the question has to be answered first - what kind of file use is the OP testing for?

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 11:16
by aGerman
Just an additional(!) test.

Code: Select all

@echo off &setlocal
set "filename=test.txt"

setlocal EnableDelayedExpansion
for /f "delims=" %%i in (
  'wmic process where "commandline like '%%!filename!%%' and name<>'cmd.exe' and name<>'wmic.exe'" get name /value'
) do for /f "delims=" %%j in ("%%i") do echo %%j
pause

Also this code will fail under several circumstances.

Regards
aGerman

Re: Any windows command to find if a file is in use or not ?

Posted: 21 Apr 2014 11:19
by Squashman
Squashman wrote:Didn't work for a text file I had opened with Notepad. But I think we have already established that Notepad will not lock the file.

But you could check Tasklist with Verbose mode to see if Notepad had the file open on the system the batch file is running.

Re: Any windows command to find if a file is in use or not ?

Posted: 22 Apr 2014 05:34
by foxidrive
Squashman wrote:But you could check Tasklist with Verbose mode to see if Notepad had the file open on the system the batch file is running.


That's interesting:

tasklist /v /fi "imagename eq notepad.exe" /fo list

Code: Select all

Image Name:   notepad.exe
PID:          6276
Session Name: Console
Session#:     1
Mem Usage:    5,388 K
Status:       Running
User Name:    PC\Me
CPU Time:     0:00:00
Window Title: time - 1 minute intervals.log - Notepad


It's too much to expect that piping the output of tasklist would locate a file in use. But it would work for some occasions.

Code: Select all

tasklist /v /fo list |findstr /L ": filename.ext - "

Re: Any windows command to find if a file is in use or not ?

Posted: 22 Apr 2014 07:57
by penpen
Although it is an interesting work, i fear you use the task "if a file is in use" in a too common way.
In case of notepad, on most text files (<= 8 MB) i doubt that notepad.exe holds the file open for more than 1 second (tested on my pc: 0.24 secs max, using process explorer):
Instead it works on a copy in memory.

So the above test (call;>>"%filename%" version) should be what is needed:
- the result may be outdated, even before you see the message,
- only unshared file accesses are detected
- shared write accesses may result in unexpected behavior (you may write to a file, and the file is replaced by another shared write.)
But all these are problems that linux also has:
These problems are the disadvantage of the fact that the filesystems are implemented as a database system (like, as far as i know, in all modern oses).
Advantage gained: Data integrity; even if system crashes without a note.

Beside:
I don't know if an editor under linux holds a text file open all time the editor is opened, but i doubt that, too:
Even if linux would try to force programs to act in such a way, i cannot imagine how this should be done if the programmer of that editor doesn't want to do that.
So lsof would report this text file only if the editor programmer has opened it all the time (or you had the luck to call Isof within the loading/saving time; but this also could happen with the above batch).

penpen