Thursday, December 13, 2012

ASP.NET Temp Files Cleanup


ASP.NET Temp Files Cleanup


@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION

REM Subhag Ghosh , Cleanup ASP.NET temp files

set PARAMETER_COUNT=0
for %%x in (%*) do set /A PARAMETER_COUNT=!PARAMETER_COUNT!+1
IF /I !PARAMETER_COUNT! lss 1 goto:BAD_PARAMETERS

IF NOT EXIST "%~1" goto:BAD_PATH
SET ROOT_DIR=%1
SET INIT_DIR=%cd%

IF /I %PARAMETER_COUNT% lss 2 (
SET IGNORE_NUM=0 
)ELSE (
SET IGNORE_NUM=%2
)

IF /I %IGNORE_NUM% lss 0 goto:BAD_IGNORE_NUM
IF /I %IGNORE_NUM% gtr 10 goto:BAD_IGNORE_NUM


SET LOG_FILE=%INIT_DIR%\ASPNETTempFilesCleanup.log


ECHO Starting folder cleanup...
ECHO %time% Starting folder cleanup... > "%LOG_FILE%"

SET MOTHER_LOOPS=0
SET MOTHER_DELETES=0
SET MOTHER_IGNORES=0
SET MOTHER_DELETE_AMOUNT=0
SET MOTHER_IGNORE_AMOUNT=0

CD /D %ROOT_DIR%

for /f "delims=" %%H in ('dir /AD /B') do (

ECHO Processing folder %%H...

CD /D %ROOT_DIR%\%%H
ECHO !time!        ======================================================================== >> "%LOG_FILE%"
ECHO !time! eSpace: %%H >> "%LOG_FILE%"
ECHO !time! Action Folder Date size >> "%LOG_FILE%"
ECHO !time! -------- -------- ---------------- -------- >> "%LOG_FILE%"
SET CHILD_LOOPS=0
SET CHILD_DELETES=0
SET CHILD_IGNORES=0
SET CHILD_DELETE_AMOUNT=0
SET CHILD_IGNORE_AMOUNT=0
for /f "delims=" %%G in ('dir /AD /B /O-D /TC') do (

CD /D %%G
SET CURR_SIZE=0
FOR /R %%Y IN (*) DO (SET /A CURR_SIZE=!CURR_SIZE!+%%~zY)
CD /D ..


IF /I !CHILD_LOOPS! gtr %IGNORE_NUM% ( 
SET /A CHILD_DELETES=!CHILD_DELETES!+1
SET /A CHILD_DELETE_AMOUNT=!CHILD_DELETE_AMOUNT!+!CURR_SIZE!
ECHO !time! Deleting %%G %%~tG !CURR_SIZE! >> "%LOG_FILE%
RD /S /Q %ROOT_DIR%\%%H\%%G
)ELSE (
SET /A CHILD_IGNORES=!CHILD_IGNORES!+1
SET /A CHILD_IGNORE_AMOUNT=!CHILD_IGNORE_AMOUNT!+!CURR_SIZE!
ECHO !time! Ignoring %%G %%~tG !CURR_SIZE! >> "%LOG_FILE%"
)
SET /A CHILD_LOOPS=!CHILD_LOOPS!+1
)

ECHO !time! ---------------------------------------------------------------- >> "%LOG_FILE%"

SET /A CONVERTED_DELETE_AMOUNT=!CHILD_DELETE_AMOUNT!/1048576
SET /A CONVERTED_IGNORE_AMOUNT=!CHILD_IGNORE_AMOUNT!/1048576

ECHO !time! !CHILD_DELETES! of !CHILD_LOOPS! folders deleted >> "%LOG_FILE%"
ECHO !time! !CONVERTED_DELETE_AMOUNT!MB deleted !CONVERTED_IGNORE_AMOUNT!MB ignored >> "%LOG_FILE%"
ECHO !time! >> "%Log_FILE%"

SET /A MOTHER_LOOPS=!MOTHER_LOOPS!+1
SET /A MOTHER_DELETES=!MOTHER_DELETES!+!CHILD_DELETES!
SET /A MOTHER_IGNORES=!MOTHER_IGNORES!+!CHILD_IGNORES!
SET /A MOTHER_DELETE_AMOUNT=!MOTHER_DELETE_AMOUNT!+!CHILD_DELETE_AMOUNT!
SET /A MOTHER_IGNORE_AMOUNT=!MOTHER_IGNORE_AMOUNT!+!CHILD_IGNORE_AMOUNT!

)

SET /A CONVERTED_DELETE_AMOUNT=!MOTHER_DELETE_AMOUNT!/1048576
SET /A CONVERTED_IGNORE_AMOUNT=!MOTHER_IGNORE_AMOUNT!/1048576
ECHO %time% Total deleted folders: %MOTHER_DELETES% >> "%LOG_FILE%"
ECHO %time% Total ignored folders: %MOTHER_IGNORES% >> "%LOG_FILE%"
ECHO %time% Total deleted data: ^~%CONVERTED_DELETE_AMOUNT%MB >> "%LOG_FILE%"
ECHO %time% Total ignored data: ^~%CONVERTED_IGNORE_AMOUNT%MB >> "%LOG_FILE%"
ECHO %time% In %MOTHER_LOOPS% eSpaces >> "%LOG_FILE%"

ECHO.

ECHO Log path: 
ECHO %LOG_FILE%

ECHO.

ECHO Summary:
ECHO deleted folders: %MOTHER_DELETES%
ECHO ignored folders: %MOTHER_IGNORES%
ECHO deleted data: ^~%CONVERTED_DELETE_AMOUNT%MB
ECHO ignored data: ^~%CONVERTED_IGNORE_AMOUNT%MB
ECHO In a total of %MOTHER_LOOPS% eSpaces


goto:END


:BAD_PARAMETERS
ECHO.
ECHO ERROR: Invalid input parameters
goto:HELP

:BAD_PATH
ECHO.
ECHO ERROR: Invalid folder path
goto:HELP

:BAD_IGNORE_NUM
ECHO.
ECHO ERROR: Invalid keepnumber
goto:HELP


:HELP
ECHO.
ECHO -------------usage-------------
ECHO ASPNETTEMPFILESCLEANUP ^ [^]
ECHO.
ECHO Description:
ECHO Delete old temporary Asp.Net folders. BE SURE TO INTRODUCE THE CORRECT TEMPORARY ASP.NET FILES PATH, BECAUSE OLD FOLDERS AND SUBFOLDERS WILL BE DELETED WITHIN THAT PATH.
ECHO.
ECHO Parameter List:
ECHO ^
ECHO Path to the Temporary ASP.NET Folders
ECHO Default folder is %windir%\Microsoft.NET\Framework^\^\Temporary ASP.NET Files
ECHO.
ECHO ^
ECHO Number of recent folders to keep, besides the newest
ECHO Default value is 0 (zero)
ECHO.
ECHO Examples:
ECHO ASPTempFilesCleanup "E:\ASP.Net Temporary Folder\"
ECHO ASPTempFilesCleanup "E:\ASP.Net Temporary Folder\" 1
ECHO.
ECHO Log:
ECHO Dumped into ^\ASPNETTempFilesCleanup.log



:END

No comments:

Post a Comment