How to close all file handles under a given folder programatically?
Solution 1:
Unlocker does claim it gives you this ability:
Simply right click the folder or file and select Unlocker
If the folder or file is locked, a window listing of lockers will appear
- Simply click
Unlock All
and you are done!
I'm not sure if it supports command-line usage.
MalwareByte's FileAssassin performs similar actions, and does support command-line usage, so you should be able to script it pretty easily.
Solution 2:
Thanks for this. We've been having some issues with open file handles causing git checkouts to fail within our Windows-based Jenkins jobs and this helped correct the problem handily. I'll throw out the basics of the technique:
Install Handle.exe on the build node. I downloaded it from here http://technet.microsoft.com/en-us/sysinternals/bb896655, unzipped it and dropped Handle.exe under C:\Windows\System32 to ensure it would be available on the default %PATH%
Install the Jenkins pre-scm-buildstep plugin: https://wiki.jenkins-ci.org/display/JENKINS/pre-scm-buildstep. This allows us to define operations before the git plugin started reaching for potentially locked files.
-
Implemented the following batch command as a pre-scm build step:
@echo off echo Cleaning up open file handles from %NODE_NAME%:%WORKSPACE%... for /f "tokens=3,6,8 delims=: " %%i in ('Handle /accepteula %WORKSPACE% ^| grep workspace') do echo Releasing file lock on E:%%k & handle -c %%j -y -p %%i
It appears to work nicely and definitely cuts down on spurious failures. I'll also note a couple gotchas I worked through:
Handler.exe has an EULA that must be accepted the first time it runs. If you're running your Jenkins agent as a service under the Local System context, this is problematic because you can't log in as that user and accept it by hand. When we tried running it from the Jenkins job, the process just hung waiting for user input and it took a few minutes to figure out why. I solved this by running it from the Jenkins job using the /accepteula flag and I recommend anyone implementing this as an automated process do the same. This happens to be a registry setting under HKEY_CURRENT_USER\Software\Sysinternals\Handle and you can also set and unset it via regedit.exe if you need to manipulate it for a specific user, but the command line option seemed easiest.
The Jenkins Batch step plugin executes batch code as if it were in a script rather than as a command line directive. Notice the escapes ( e.g. %%i, ^| ).
Thanks!
Solution 3:
We use the following snippet to close file handle from users to our server. You may be able to modify it for your use.
rem close all network files that are locked
for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close
Solution 4:
I create this little batch file to auto release all locks to xml file by my eclipse
@echo off
for /f "tokens=3,6,8 delims=: " %%i in ('handle -p eclipse e:\git\ ^| grep .xml') do echo Releasing %%k & handle -c %%j -y -p %%i
You need to download the handle utility from Microsoft site and grep utility from GnuWin32
If you don't need filter by file type, you can skip grep part like this:
@echo off
for /f "tokens=3,6,8 delims=: " %%i in ('handle -p eclipse e:\git\') do echo Releasing %%k & handle -c %%j -y -p %%i
Or if you don't need to filter locks by a particular program, just remove the eclipse process filter:
@echo off
for /f "tokens=3,6,8 delims=: " %%i in ('handle e:\git\') do echo Releasing %%k & handle -c %%j -y -p %%i
Remember to replace e:\git\
with your folder path.