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.)