Advertisement
aveyo

Close_Handles_ContextMenu_FilesAndFoldersUnlocker

Sep 25th, 2018
1,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.73 KB | None | 0 0
  1. @echo off &Title 'Close Handles' context menu to unlock files or folders by AveYo v2019.09.27
  2. :: changelog: fix dl; add /accepteula; check S-1-5-19 for admin; ask for admin rights to catch system handles; auto-hide window
  3.  
  4. :: add_remove whenever script is run again
  5. reg query "HKCU\Software\Classes\Directory\shell\CloseHandles" /v MuiVerb >nul 2>nul && (
  6.  reg delete HKCU\Software\Classes\Directory\shell\CloseHandles /f >nul 2>nul
  7.  reg delete HKCU\Software\Classes\Drive\shell\CloseHandles /f >nul 2>nul
  8.  reg delete HKCU\Software\Classes\*\shell\CloseHandles /f >nul 2>nul
  9.  color 0c &echo. &echo  REMOVED! Run script again to add 'Close Handles' context menu
  10.  timeout /t -1 &color 0f &title %COMSPEC% &exit/b
  11. )
  12.  
  13. :: check if handle tool exists and if not warn about needing admin rights to install it
  14. set "URL=https://download.sysinternals.com/files/Handle.zip"
  15. pushd %TEMP%
  16. if not exist "%WINDIR%\handle.exe" (set "DOWNLOAD=1") else goto has_handle
  17. reg query HKU\S-1-5-19 >nul 2>nul || (
  18.  color 0e
  19.  echo.
  20.  echo  PERMISSION DENIED! Right-click %~nx0 ^& Run as administrator
  21.  echo  or manually download and unzip %URL%
  22.  echo  then copy handle.exe to %WINDIR%
  23.  echo.
  24. )
  25.  
  26. :: try to download handle tool from sysinternals (microsoft)
  27. pushd %TEMP%
  28. for /f "tokens=3 delims=. " %%i in ('bitsadmin.exe /create /download Handle.zip') do set "JOB=%%i"
  29. call bitsadmin /transfer %%JOB%% /download /priority foreground %%URL%% "%TEMP%\handle.zip" >nul 2>nul
  30. call bitsadmin /complete %%JOB%% >nul 2>nul
  31. set "PS_DOWNLOAD=[Net.ServicePointManager]::SecurityProtocol='tls12,tls11,tls';(new-object System.Net.WebClient).DownloadFile"
  32. if not exist handle.zip powershell -nop -c "%PS_DOWNLOAD%('%URL%','handle.zip')"
  33. if not exist handle.zip certutil -URLCache -split -f "%URL%" >nul 2>nul             &REM naked Windows 7 workaround
  34. set "PS_UNZIP=$s=new-object -com shell.application;foreach($i in $s.NameSpace($zip).items()){$s.Namespace($dir).copyhere($i)}"
  35. if exist handle.zip powershell -nop -c "$dir='%TEMP%'; $zip='%TEMP%\handle.zip'; %PS_UNZIP%"
  36. timeout /t 3 /nobreak >nul
  37. copy /y handle.exe "%WINDIR%\" 2>nul
  38. :has_handle
  39.  
  40. :: command to run
  41. set "s1=echo ''[ CLOSE HANDLES BY AVEYO ]''; $s=''%%1'';if(([IO.FileInfo]$s).Mode -notmatch ''d''){$s=($s -split ''\\'')[-1]};"
  42. set "s2=$o=New-Object Collections.ArrayList; handle.exe /accepteula -a -u $s| foreach {if ($_ -match ''pid: "
  43. set "s3=(?<P>\d*)\s.*\s(?<H>[\da-z]*):\s*(?<F>[\\a-z]:\\.*)''){$m=''''|select ''O'',''P'',''H'',''F''; $m.P=$matches[''P'']; "
  44. set "s4=$m.H=$matches[''H'']; $m.F=$matches[''F'']; $m.O=$_.Substring(0, $_.lastIndexOf($m.H)); $o.Add($m) | Out-Null} };"
  45. set "s5=$o | Out-GridView -Title ''Close handles'' -PassThru | foreach{handle.exe /accepteula -p $_.P -c $_.H -y}"
  46. set "snipp=powershell -w hidden -c start powershell -ArgumentList '-c %s1% %s2% %s3% %s4% %s5%' -verb RunAs -windowstyle hidden"
  47.  
  48. :: generate context menu
  49. (
  50. reg add HKCU\Software\Classes\Directory\shell\CloseHandles /v MuiVerb /d "Close Handles" /f
  51. reg add HKCU\Software\Classes\Directory\shell\CloseHandles /v HasLUAShield /d "" /f
  52. reg add HKCU\Software\Classes\Directory\shell\CloseHandles\command /ve /d "%snipp%" /f
  53. reg add HKCU\Software\Classes\Drive\shell\CloseHandles /v MuiVerb /d "Close Handles" /f
  54. reg add HKCU\Software\Classes\Drive\shell\CloseHandles /v HasLUAShield /d "" /f
  55. reg add HKCU\Software\Classes\Drive\shell\CloseHandles\command /ve /d "%snipp%" /f
  56. reg add HKCU\Software\Classes\*\shell\CloseHandles /v MuiVerb /d "Close Handles" /f
  57. reg add HKCU\Software\Classes\*\shell\CloseHandles /v HasLUAShield /d "" /f
  58. reg add HKCU\Software\Classes\*\shell\CloseHandles\command /ve /d "%snipp%" /f
  59. ) >nul
  60.  
  61. :: done
  62. echo.
  63. echo  ADDED! Run "%~nx0" again to remove 'Close Handles' context menu
  64. timeout /t -1
  65. exit/b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement