One of our departments needed to upgrade software on a number of their servers, but it is an application that users like to leave running when they leave at night, and the connected files keep the upgrade from working. They needed to close thousands of open files in the application shares on several hundred servers.
They asked me if there’s a way to close all of the connected files in the application shares on those servers. I put together this little batch script to do just that.
@echo off (set share=d:\share\) REM !! IMPORTANT set length in 4th from last line !! FOR /f "Skip=4 tokens=1,2" %%i in ('NET FILES') do ( IF /I NOT "%%i"=="The" ( call :checkpath %%i "%%j" ) ) ::pause GOTO :EOF :checkpath set id=%1 set folder=%~2\ :: !! The last number in the next line needs to :: be the length of the SHARE string SET root=%FOLDER:~0,9% IF /I NOT "%ROOT%"=="%SHARE%" GOTO :EOF echo %ID% is open on %FOLDER%. Closing... NET FILE %id% /CLOSE |
This was run directly from the package that pushes the installer, but it could just as easily be pushed in any other manner. You could also use it on a schedule to drop open locks. You just need to uncomment the pause if you want to see the results. (the :: is the equivalent of REM in batch, except it actually runs faster.)
2 responses so far ↓
1 Christian // Oct 20, 2008 at 2:55 am
how to deal with long Paths to the Share?
net files displays c:\share\…\file
Reply to Christian
2 Neil // Nov 13, 2008 at 12:12 pm
Christian,
Just make sure you match the SHARE variable to whatever the path to the share is, ie:
(SET SHARE=c:\share\…\)
and make sure you set the length near the end:
SET root=%FOLDER:~0,9%
‘9′ should be whatever the length of SHARE is. That line chops off the beginning of the files returned from NET FILES, and compares it to the SHARE you set. If it matches, it’s dropped, and if it doesn’t match it’s ignored.
The upshot is that anything below the path you set in SHARE will be closed. (Technically, any file whose path begins with that string.)
HTH
Reply to Neil
Leave a Comment