大掃除


Windows Update 後、システムドライブの空き容量が逼迫する傾向にあったので、一時しのぎに以下の記事を参考にして書きました。

 C:\Windows\Temp フォルダーに cab_XXX_X(X は数字)が生成されディスクの容量を圧迫している場合の対処方法

cleanup.bat
@echo off

openfiles > NUL 2>&1
if %ERRORLEVEL% equ 1 (
  powershell.exe -Command Start-Process \"%~f0\" -Verb runas
  exit /b
)

:: ref.
::   https://blogs.technet.microsoft.com/askcorejp/2015/04/15/cwindowstemp-cab_xxx_x-x-37327/

set SELF=%~n0
set FILE=%TEMP%\%SELF%.log

setlocal
  :: %SYSTEMROOT%\Temp\cab_*_*
  call :delete "%SYSTEMROOT%\Temp" "cab_*_*"

  :: %SYSTEMROOT%\logs\CBS\CbsPersist_*.log
  call :delete "%SYSTEMROOT%\logs\CBS" "CbsPersist_*.log"

  :: %SYSTEMROOT%\logs\CBS\CbsPersist_*.cab
  call :delete "%SYSTEMROOT%\logs\CBS" "CbsPersist_*.cab"

  echo.
  set /p _=Press any key to close this window. 
endlocal
goto :EOF

:delete
setlocal enabledelayedexpansion
  echo.
  echo %~1\%~2

  if not exist "%~1\" exit /b 1

  forfiles /p %~1 /m "%~2" /c "cmd /c echo @file,@fsize,@fdate,@ftime" > %FILE% 2> NUL && (
    echo.
    for /f "tokens=1-4 delims=," %%a in (%FILE%) do (
      set b=                    %%b
      set d=0%%d
      echo ^ ^ %%~a: !b:~-15! bytes^ ^(%%c !d:~-8!^)
    )
    call :Q "Are you sure you want to delete the above file(s)?" && (
      echo.
      del /q "%~1\%~2" && (
        echo ^ ^ Delete successful.
      ) || (
        echo ^ ^ Delete failed.
      )
    ) || (
      echo.
      echo ^ ^ Canceled.
    )
  ) || (
    echo.
    echo ^ ^ File not found.
  )

  del %FILE% > NUL 2>&1
endlocal
goto :EOF

:Q
setlocal
  :BOL_Q
    echo.
    set /p A=%~1 ^(Y/n^) 
    if /i "%A%" == "Y" exit /b 0
    if /i "%A%" == "N" exit /b 1
  goto :BOL_Q
endlocal
goto :EOF

実行結果は以下のとおりです。