Last active
June 18, 2020 23:06
-
-
Save jkniiv/f60cfca758d1fa98466dfd915c5160be to your computer and use it in GitHub Desktop.
On Windows filter handle.exe output to list any open files on a certain drive (yes I know you can use findstr but this one prints also the process name)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { | |
# drive = "E"; | |
print "* The ‘drive’ parameter as specified was ‘" drive "’ (variable value in single quotes)"; | |
# print "* Searching for all references to " drive ":\\ in the third column of the input"; | |
} | |
/pid:/ { | |
pidline = $0; | |
} | |
/\w:\\/ { | |
drivepat = "(?i)\\b" drive ":\\\\"; | |
if ($3 ~ drivepat) { | |
print pidline; | |
# print drivepat; | |
print $0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
set drive=%1 | |
if not defined drive goto :usage | |
:: remove possible colon character at the end to make 'drive' a bare letter | |
if "%drive:~-1%"==":" ( | |
set drive=%drive:~0,-1% | |
) | |
rem :: in case 'drive' is now empty, give some usage | |
if "%drive%"=="" goto :usage | |
:: Invoke 'handle' and pipe it to 'goawk' to give us the awk language goodies for matching :) | |
:: Note that we use 'iconv' to make sure the encoding is UTF-8 which is goawk's internal encoding. | |
echo * Searching for all references to %drive%:\ in the third column of handle.exe output | |
handle | iconv -f WINDOWS-1252 -t UTF-8 | goawk -v drive=%drive% -f %~dpn0.awk | |
goto :EOF | |
:usage | |
echo Please invoke me thusly: %~nx0 ^<drive letter^> | |
echo for instance: | |
echo %~nx0 D | |
echo or | |
echo %~nx0 D: | |
echo. | |
echo The colon in the drive letter is entirely optional :^) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you can see above, I use GoAWK to interpret lsofdrv.awk in the corresponding batch file. GNU AWK aka. Gawk could work but I haven't tested it. Also as per above, if you choose to use GoAWK and your file names have characters beyond ASCII, you need to translate handle.exe's Windows-specific output encoding (in my case codepage 1252) to GoAWK's UTF-8 to keep the output of such things as umlauts (ä, ö) or other extended characters readable. For that you need iconv which can be installed via Scoop or manually if you so please.