0

I am writing a Windows shell script that works with lock files. For the sake of knowing if some file is locked, I am testing the Handle* utility from SysInternals that, according to its docs:

«Handle is a utility that displays information about open handles for any process in the system. You can use it to see the programs that have a file open»

so I try it by creating a simple .txt file:

C:\Windows\system32>echo Foo >> Foo.txt
C:\Windows\system32>notepad Foo.txt

(notepad appears on screen with a new file named Foo.

C:\Windows\system32>handle Foo

Handle v3.51
Copyright (C) 1997-2013 Mark Russinovich
Sysinternals - www.sysinternals.com

No matching handles found.

What is going on here? My file is supposed to be open, and notepad should have a handle on it, but this line:

handle -p notepad | grep "Foo"

yields no results.
How can I use handle to know if my Foo.txt file is in use (locked)?
Maybe someone could give me some examples of the usage of handle.

Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
  • Notepad is a simple program, is slurps the entire file into memory and closes the file handle. Easy to see, you can delete the file while Notepad is allowing you to edit it. You'd need a *really* large file to have enough time to get Handle started. – Hans Passant Mar 23 '14 at 16:32
  • You are right, @HansPassant. So, I need to understand exactly in what cases, when programming from command-line Windows shell, a file will be detected by **Handle** as `in use`, or whatever this program detects. Because I suppose now that a simple `echo InUse>>c:\Temp\FileLock.lck` will not enough. Could you please give some orientation or documentation? Thanks. – Sopalajo de Arrierez Mar 23 '14 at 18:48
  • Perhaps you could ask a new question? "How do I hold a file open from a Windows command script?" – Harry Johnston Mar 23 '14 at 21:25
  • Done, @HarryJohnston: http://stackoverflow.com/questions/22597647/how-do-i-hold-a-file-open-locked-from-a-windows-command-line-shell-script – Sopalajo de Arrierez Mar 23 '14 at 21:57

2 Answers2

6

Notepad reads the file into memory and closes the handle, which is why you don't see it open. You can see that behavior in a Process Monitor trace.

  • Thanks, @MarkRussinovich. So, what can I use from Windows command-line to lock a file in any manner that will be detected by **Handle**? Echo? Edit? More? Maybe some CygWin command? – Sopalajo de Arrierez Mar 23 '14 at 18:51
  • @MarkRussinovich - Maybe you can help me with this: http://stackoverflow.com/questions/30504972/dump-output-of-sysninternals-handle-exe-to-a-text-file – Gabriel May 28 '15 at 11:16
0

At first I guess that you forgot the file extension .txt in your command line, the second thing is I guess it will only show processes with a exclusive file lock which is not given by notepad.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • Thanks, @rekire. As long as the original idea is to lock a file by Windows command-line, and the original question is *How to use Handle to know if my file is locked?*, what simple test could I try, then? – Sopalajo de Arrierez Mar 23 '14 at 18:54
  • Try some open word files or some dll files which are in use. – rekire Mar 23 '14 at 18:56