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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

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

#1 Post by sambasiva » 21 Apr 2014 01:01

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#2 Post by foxidrive » 21 Apr 2014 02:17

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

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

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

#3 Post by sambasiva » 21 Apr 2014 03:04

Thanks for the reply.

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

Any idea what does "call;" means ?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 21 Apr 2014 03:32

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#5 Post by foxidrive » 21 Apr 2014 03:53

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#6 Post by aGerman » 21 Apr 2014 04:08

"locked for writing" was just loosely speaking. We could do some tests with CreateFile and its share modes.

Regards
aGerman

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#7 Post by Squashman » 21 Apr 2014 07:11

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

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

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

#8 Post by miskox » 21 Apr 2014 08:54

This might work:

Code: Select all

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


Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#9 Post by foxidrive » 21 Apr 2014 09:00

That works in my test with c:\swapfile.sys

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#10 Post by Squashman » 21 Apr 2014 09:13

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#11 Post by foxidrive » 21 Apr 2014 09:34

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#12 Post by aGerman » 21 Apr 2014 11:16

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#13 Post by Squashman » 21 Apr 2014 11:19

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#14 Post by foxidrive » 22 Apr 2014 05:34

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

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

#15 Post by penpen » 22 Apr 2014 07:57

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

Post Reply