(view source code of wholocked.bat as plain text)
@ECHO OFF
:: Check command line arguments
IF "%~1"=="" GOTO Syntax
IF "%~1"=="/?" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
IF NOT EXIST "%~1" GOTO Syntax
:: Check if handle.exe is available
HANDLE.EXE /? >NUL 2>&1 || GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2 delims=:" %%A IN ('HANDLE.EXE -nobanner -accepteula "%~1"') DO (
SET LockingProg=%%A
SET LockingProg=!LockingProg: pid=!
)
:: Remove trailing spaces
:WhileLoop
IF "%LockingProg:~-1%"==" " (
SET LockingProg=%LockingProg:~0,-1%
GOTO WhileLoop
)
ECHO "%~1" is blocked by %LockingProg%
ECHO Do you want to ←[0;1;31mC←[0mlose it, ←[0;1;31mK←[0mill it or ←[0;1;31mI←[0mgnore it? [CKI]
CHOICE.EXE /C CKI /N /D I /T 30
SET Choice=%ErrorLevel%
IF %Choice% EQU 1 (
TASKKILL.EXE /IM %LockingProg%
)
IF %Choice% EQU 2 (
TASKKILL.EXE /IM %LockingProg% /F
)
ENDLOCAL
EXIT /B 0
:Syntax
ECHO.
ECHO WhoLocked.bat, Version 1.00
ECHO Check which program keeps a file or folder locked and optionaly unlock it
ECHO.
ECHO Usage: ←[0;1mWhoLocked.bat file_or_folder←[0m
ECHO.
ECHO Where: ←[0;1mfile_or_folder←[0m is the file or folder being locked
ECHO.
ECHO Notes: This batch file uses Mark Russinovich's HANDLE.EXE, available at
ECHO ←[0;1;30mhttps://learn.microsoft.com/en-us/sysinternals/downloads/handle←[0m
ECHO to check which program keeps the file or folder open.
ECHO Though this could also be achieved with Windows' native
ECHO OPENFILES.EXE, HANDLE.EXE does not require elevated privileges
ECHO whereas OPENFILES.EXE does.
ECHO This batch file uses Windows' native TASKKILL.EXE to terminate
ECHO the blocking program. This could also be achieved with HANDLE.EXE,
ECHO but that would require elevated privileges, whereas TASKKILL.EXE
ECHO does not.
ECHO The batch file gives you a choice to close the locking program
ECHO gracefully, or to kill it by force, or to ignore it and do nothing.
ECHO In case multiple programs keep the file or folder locked, only the
ECHO ←[0;1mlast one found←[0m is displayed, so you may have to run this batch file
ECHO multiple times.
ECHO.
ECHO Written by Rob van der Woude
ECHO https://www.robvanderwoude.com
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0078 seconds