The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,805 other followers

Deploying .EXE on a server share when it is in use by workstations (using Handles.exe and for)

Posted by jpluimers on 2010/06/15

In a lot of network environments, you (still ) find a situation where workstations run a .EXE from a network share.

In these situations, you cannot update the .EXE on the server: you get “Access Denied” errors.

Below is a simple batch-file trick to overcome this.
It kills all the handles that the workstations have to the .EXE
After you have run this batch-file, you can copy the .EXE.

There still is a small race condition: if between you kill the handles and copying the new .EXE, someone starts the old .EXE

The trick below is to find the handles of the .EXE, and then close these handles.

The first step uses Handle.exe from SysInternals.

For the second step , the tricks is to parse the list of active handles produced by Handle.exe.

You can use the for command for parsing. The link has a section that explains “iterating and file parsing” using the for command.
Microsoft has a KB article explaining a similar usage for logging off all terminal server session users from the command prompt.

Below is an example that parses the output from handle.exe:

C:\MyApplication\Version1\bin>c:\bin\handle.exe MyApplication.exe > usage.txt
C:\MyApplication\Version1\bin>type usage.txt
Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals -

System             pid: 4       87C: C:\MyApplication\Version1\bin\MyApplication.exe

C:\MyApplication\Version1\bin>for /F "skip=4 tokens=3,4 delims=: " %1 in (usage.txt) do echo -p %1 -c %2 -y
C:\MyApplication\Version1\bin>echo -p 4 -c 87C -y
-p 4 -c 87C -y

What you see is that we

  1. skip 4 lines,
  2. split the line on colon (:) boundaries,
  3. take the 3rd and 4th token from each line (tokens: 1=System, 2=Pid, 3=Pid-Value, 4=Handle)

Here is the complete batch-file that kills all handles referring to MyApplication.exe:

c:\bin\handle.exe MyApplication.exe > usage.txt
type usage.txt
for /f "skip=4 tokens=3,4 delims=: " %%1 in (usage.txt) do c:\bin\handle.exe -p %%1 -c %%2 -y
del usage.txt

And this is the deployment batch-file:

net use \\SERVER\c$ /user:USER Pa$$w0rd
copy \\SERVER\c$\MyApplication\version1\bin\MyApplication.*
net use \\SERVER\c$ /d

Hope this helps you when you need to use the for command or when you want to kill handles to open processes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: