::------------------------------------------------------------------------------------------------::
:: Script: DelOldUsers.Bat ::
:: Version: 1.3 ::
:: Copyright: Ben Personick ::
:: Date: 2010-07-14 ::
:: ::
:: ::
:: Desc: Deletes User Directories Older than N Days ::
:: ::
::------------------------------------------------------------------------------------------------::
:Begin
Echo Off
:Start-Prep
SET ScriptName=DelOldUsers.Bat
CALL :Start-Set-Date
ECHO %TDate% - %TTime% -- Beginging Preparation for Script: %ScriptName%
SET OldSysDrv=C:
::FOR XP SET OldSysUsrDir=Documents and Settings
SET OldSysUsrDir=Users
SET OldSysBase=%OldSysDrv%\%OldSysUsrDir%
SET MaxAgeNTUser=60
:: Set the Upgrade Day Variable
SET UDay=%date:~7,2%
:: Set the Upgrade Month Variable
SET UMonth=%date:~4,2%
:: Set the Upgrade Year variable
SET UYear=%date:~10,4%
CALL :Start-Number-Date %UDay% %UMonth% %UYear%
SET CurrentDTotal=%DTotal%
CALL :Start-Set-Date
ECHO %TDate% - %TTime% (In days: %CurrentDTotal%) -- Preperation Complete - Script: %ScriptName% Begining
:End-Prep
:Start-Main
CALL :Start-Set-Date
ECHO %TDate% - %TTime% -- Begining to loop through User Directories in %OldSysBase%
For /F "Tokens=*" %%B IN ('dir /A:D /B "%OldSysBase%\*"') DO CALL :Start-UserDir-Loop "%%B"
GOTO End-UserDir-Loop
:Start-UserDir-Loop
::Skip needed system folders from being evaluated in Win XP
IF /I %1=="All Users" GOTO :EOF
IF /I %1=="Default User" GOTO :EOF
::Skip needed system folders from being evaluated in Win Vista+
IF /I %1=="Public" GOTO :EOF
IF /I %1=="Default" GOTO :EOF
::Set TUser variable to = Username without Quotes
SET TUser=%~1
CALL :Start-Set-Date
ECHO %TDate% - %TTime% -- Looking for NTUser.dat older than %MaxAgeNTUser% Days In "%OldSysBase%\%TUser%"
For /F "tokens=1-3" %%U IN ('dir "%OldSysBase%\%TUser%\NTUSER.dat" /A:H ^| Find /I "NTUser.dat"') DO CALL :Start-NTUser-Loop "%%U"
GOTO End-NTUser-Loop
:Start-NTUser-Loop
SET NDate=%~1
CALL :Start-Set-Date
IF /I "%TUser%"=="%NDate%" ECHO %TDate% - %TTime% -- CANNOT Locate NTUser.Dat! Skipping "%OldSysBase%\%TUser%"!
IF /I "%TUser%"=="%NDate%" GOTO :EOF
:: Set the Upgrade Day Variable
SET NDay=%NDate:~3,2%
:: Set the Upgrade Month Variable
SET NMonth=%NDate:~0,2%
:: Set the Upgrade Year variable
SET NYear=%NDate:~6,4%
CALL :Start-Number-Date %NDay% %NMonth% %NYear%
SET NTUserDate=%DTotal%
:: HERE WE WILL CALL A COMPARE FUNCTION!
CALL :Start-Compare-Date %CurrentDTotal% %NTUserDate%
SET IsAge=%TDifference%
IF %IsAge% GEQ %MaxAgeNTUser% CALL :Start-Delete-Directory "%OldSysBase%\%TUser%"
REM ECHO %TDate% - %TTime% -- HERE WE WILL CALL A COMPARE FUNCTION! for %NDay% and %NMonth% and %NYear%
GOTO :EOF
:End-NTUser-Loop
GOTO :EOF
:End-UserDir-Loop
GOTO End
:End-Main
GOTO End-Subs
GOTO End
:Start-Subs
GOTO End-Set-Date
:Start-Set-Date
SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
GOTO :EOF
:End-Set-Date
GOTO End-Number-Date
:Start-Number-Date
SET DDay=%1
IF %DDay:~0,1%==0 SET DDAY=%DDay:~1,1%
REM ECHO DDay = %DDay%
IF %2 NEQ 02 GOTO End-Leap-Day
:Start-Leap-Day
IF %1 GTR 28 SET DDay=28
:End-Leap-Day
REM ECHO DDay = %DDay%
SET DMonth=0
REM ECHO DMonth = %DMonth%
IF %2 GTR 01 SET DMonth=31
REM ECHO DMonth = %DMonth%
IF %2 GTR 02 SET /A DMonth=%DMonth%+28
REM ECHO DMonth = %DMonth%
IF %2 GTR 03 SET /A DMonth=%DMonth%+31
REM ECHO DMonth = %DMonth%
IF %2 GTR 04 SET /A DMonth=%DMonth%+30
REM ECHO DMonth = %DMonth%
IF %2 GTR 05 SET /A DMonth=%DMonth%+31
REM ECHO DMonth = %DMonth%
IF %2 GTR 06 SET /A DMonth=%DMonth%+30
REM ECHO DMonth = %DMonth%
IF %2 GEQ 07 SET /A DMonth=%DMonth%+31
REM ECHO DMonth = %DMonth%
IF %2 GEQ 08 SET /A DMonth=%DMonth%+31
REM ECHO DMonth = %DMonth%
IF %2 GEQ 09 SET /A DMonth=%DMonth%+30
REM ECHO DMonth = %DMonth%
IF %2 GEQ 10 SET /A DMonth=%DMonth%+31
REM ECHO DMonth = %DMonth%
IF %2 GEQ 11 SET /A DMonth=%DMonth%+30
REM ECHO DMonth = %DMonth%
SET /A DYear=%3*365
REM ECHO DYear = %DYear%
REM ECHO DTotal = %DDay% + %Dmonth% + %DYear%
REM ECHO %DTotal%
SET /A DTotal=%DDay%+%Dmonth%+%DYear%
REM ECHO DTotal = %DTotal%
GOTO :EOF
:End-Number-Date
GOTO End-Compare-Date
:Start-Compare-Date
SET Date1=%1
SET Date2=%2
ECHO Date1 = %Date1% Date2 = %Date2%
SET /A TDifference=%Date1%-%Date2%
ECHO Age Difference in days = %TDifference%
GOTO :EOF
:End-Compare-Date
GOTO End-Delete-Directory
:Start-Delete-Directory
ECHO Now Running RMDir /S /Q %1
RMDir /S /Q %1
GOTO :EOF
:End-Delete-Directory
GOTO End
:End-Subs
:End
CALL :Start-Set-Date
ECHO %TDate% - %TTime% -- Script %ScriptName% Exiting!
GOTO :EOF
|