Delete Files Older Then x-Days v.3

February 18, 2009 at 12:16:28
Specs: Windows XP SP2, 256
Good day.

Thank you for considering my program.

Goals:
This program will delete files no longer wanted on a windows computer system.
By specifying the type of file (extension(s)), location, and an "older then" value,
files will be deleted.
The program can be run with an automated program (Task Scheduler).

Overview:
This program has been evolving over the past several years.
Yes, this program has been written in DOS code. There are other programming languages, which maybe
capable of this type of operation, but this is my creation and I choose the language I know best.

Additionally, since the code is not compiled, anyone can change the code to suit their particular
usage. I have attempted to program all reasonable options into the program while still keeping it
user friendly (and automated task friendly)

There are similar programs out there (in cyberspace) to maintain (clean - delete) files,
but this version has a special trick. You can tell it which files to KEEP!
Sure, deleting all LOG files is easy. But, what do you do, if you want to delete all files,
EXCEPT the LOG files? Can you make it automated, also? Can you keep multiple types of files
(LOG,ZIP,TXT,DLL...) and delete all the rest which are only older then X (insert a number) of days old?

Anyways, you get the point. Please read the Help Section.

Contact Information:
Feel free to contact me with any question comments or concerns.
Name: Pete Soski
Email: SoskiP@Yahoo.com

HELP SECTION:
a) Double Click the Purge_Files.bat program and it will bring up the Help Section.
b) Use any text editor to open the file and scan down to line 440.

Text Editor I recommend is Portable Apps, NotePAD++ (any version).

Main Site:
http://portableapps.com/
Direct Link:
http://portableapps.com/news/2009-0...

========================
=== START OF PROGRAM ===
=== Copy and paste ===
=== Into a text file ===
=== Rename to *.bat ===
========================

REM Written by: Pete Soski
REM Last Revision Date: 18 February 2009
REM Contact: SoskiP@Yahoo.com
:START
@ECHO OFF
Title Purge_Files.bat - Remove Unwanted Files
CLS
ECHO.
ECHO.
REM the follow six lines check for User Help Requests
SET Help_A=%~1
SET Help_B=%Help_A:~0,2%
IF "%Help_B%"=="~0,2" (GOTO Error)
IF "%Help_B%"=="/?" (GOTO Error)
IF /I "%Help_B%"=="-h" (GOTO Error)
IF /I "%Help_B%"=="/h" (GOTO Error)
REM End User Help Request Checking.

GOTO Get_Time

:Get_Time
SET Start_Time=%time%
FOR /f "tokens=1-3 delims=:." %%a IN ('ECHO %time%') do (
SET /a Start_hh=%%a
SET /a Start_mm=%%b
SET /a Start_ss=%%c)
FOR /f "tokens=1-3 delims=: " %%a IN ('time /t') do (SET Start_Meridian=%%c)
ECHO Start Time: %Start_hh%:%Start_mm%:%Start_ss% %Start_Meridian%
FOR /f "tokens=1-4 delims=/ " %%a IN ('date /t') do (
SET xx=%%a
SET mm=%%b
SET dd=%%c
SET /A yyyy=%%d)
GOTO Presets

:Presets
SET Prog=DEL
SET DEXT=0
SET SEXT=0
SET DelEXT=*
SET Space=
SET SubDir=No
SET DelRO=No
SET /A Delete_Counter=0
SET /A FChecked_Counter=0
SET /A Total_Files=0
SET /A Finish_day=0

REM These Variables contain the user settings
SET Sw1=%1
SET Sw2=%2
SET Sw3=%3
SET Sw4=%4
SET Sw5=%5
SET Sw6=%6
SET Sw7=%7
SET Sw8=%8
SET Sw9=%9
GOTO Evaluate

:Evaluate
REM Start Evaluate Switches
IF "" NEQ "%Sw1%" (Call :QualifySwitches %Sw1%)
IF "" NEQ "%Sw2%" (Call :QualifySwitches %Sw2%)
IF "" NEQ "%Sw3%" (Call :QualifySwitches %Sw3%)
IF "" NEQ "%Sw4%" (Call :QualifySwitches %Sw4%)
IF "" NEQ "%Sw5%" (Call :QualifySwitches %Sw5%)
IF "" NEQ "%Sw6%" (Call :QualifySwitches %Sw6%)
IF "" NEQ "%Sw7%" (Call :QualifySwitches %Sw7%)
IF "" NEQ "%Sw8%" (Call :QualifySwitches %Sw8%)
IF "" NEQ "%Sw9%" (Call :QualifySwitches %Sw9%)
GOTO Process_Time

:QualifySwitches
SET Check1=%~1
SET Check2=%Check1:~0,1%
SET Check3=%Check1:~0,5%
SET Check4=%Check1:~0,4%
SET Check5=%Check1:~0,2%
IF %Check2%==/ (SET DEXT=0)
IF %Check2%==/ (SET SEXT=0)
IF %DEXT%==1 (GOTO ParseDEXT)
IF /I %Check3%==/DEXT (
SET Dext=1
GOTO ParseDEXT)
IF %SEXT%==1 (GOTO ParseSEXT)
IF /I %Check3%==/SEXT (
SET SEXT=1
GOTO ParseSEXT)
IF /I %Check1%==/SUB (SET SubDir=Yes)
IF /I %Check1%==/ROD (SET DelRO=Yes)
IF /I %Check1%==/Test (SET PROG=DIR)
IF /I %Check3%==/PATH (SET StartIn=%Check1:~6%)
IF /I %Check4%==/DAY (SET Days=%Check1:~5%)

REM The following (if) performs quality checks on the user Settings
IF %Check2% NEQ / (
ECHO This entry is not Valid: %Check1%
GOTO END)
GOTO END

:ParseSEXT
SET Check=%Check1:~0,5%
SET EXT=%Check1:~6%
IF /I %Check% NEQ /SEXT (SET SaveEXT=%SaveEXT%%Space%%Check%
) ELSE (SET SaveEXT=%EXT%)
GOTO END

:ParseDEXT
SET Check=%Check1:~0,5%
SET EXT=%Check1:~6%
IF "%DelEXT%"=="*" (SET DelEXT=)
IF /I %Check% NEQ /DEXT (SET DelEXT=%DelEXT%%Space%%Check%
) ELSE (SET DelEXT=%EXT%)
GOTO END

:Process_Time
IF %mm%==01 SET /A mm=1
IF %mm%==02 SET /A mm=2
IF %mm%==03 SET /A mm=3
IF %mm%==04 SET /A mm=4
IF %mm%==05 SET /A mm=5
IF %mm%==06 SET /A mm=6
IF %mm%==07 SET /A mm=7
IF %mm%==08 SET /A mm=8
IF %mm%==09 SET /A mm=9
IF %mm%==10 SET /A mm=10
IF %mm%==11 SET /A mm=11
IF %mm%==12 SET /A mm=12
IF %dd%==01 SET dd=1
IF %dd%==02 SET dd=2
IF %dd%==03 SET dd=3
IF %dd%==04 SET dd=4
IF %dd%==05 SET dd=5
IF %dd%==06 SET dd=6
IF %dd%==07 SET dd=7
IF %dd%==08 SET dd=8
IF %dd%==09 SET dd=9
SET /A dd=%dd%+0
REM The following (SET) adjusts the dd variable to minimum age to delete.
SET /A dd=%dd% - %Days%
SET /A yyyy=%yyyy%+0

:LOOPDATE
IF /I %dd% GTR 0 GOTO DONE
SET /A mm=%mm% - 1
IF /I %mm% GTR 0 GOTO ADJUSTDAY
SET /A mm=12
SET /A yyyy=%yyyy% - 1

:ADJUSTDAY
IF %mm%==1 GOTO SET31
IF %mm%==2 GOTO LEAPCHK
IF %mm%==3 GOTO SET31
IF %mm%==4 GOTO SET30
IF %mm%==5 GOTO SET31
IF %mm%==6 GOTO SET30
IF %mm%==7 GOTO SET31
IF %mm%==8 GOTO SET31
IF %mm%==9 GOTO SET30
IF %mm%==10 GOTO SET31
IF %mm%==11 GOTO SET30
IF %mm%==12 GOTO SET31

:SET31
SET /A dd=31 + %dd%
GOTO LOOPDATE

:SET30
SET /A dd=30 + %dd%
GOTO LOOPDATE

:LEAPCHK
SET /A tt=%yyyy% %% 4
IF not %tt%==0 GOTO SET28
SET /A tt=%yyyy% %% 100
IF not %tt%==0 GOTO SET29
SET /A tt=%yyyy% %% 400
IF %tt%==0 GOTO SET29

:SET28
SET /A dd=28 + %dd%
GOTO LOOPDATE

:SET29
SET /A dd=29 + %dd%

:DONE
IF /i %dd% LSS 10 SET dd=0%dd%
IF /I %mm% LSS 10 SET mm=0%mm%
GOTO MAIN

:Main
IF /I %SubDir%==Yes (SET Sub=/S)
ECHO.
ECHO This is the program to run: %Prog%
ECHO This is the PATH: %StartIn%
ECHO This is the number of days to save: %Days%
ECHO Should Sub-Directories be searched?: %SubDir%
ECHO Should Read-Only Files be deleted?: %DelRO%
ECHO These file types will be Deleted: %DelEXT%
ECHO These file types will be Saved: %SaveEXT%
ECHO.
ECHO Starting Main...
ECHO.

IF /i "" NEQ "%SaveEXT%" (GOTO ProcessFile_Sext)
IF /i "" NEQ "%DelEXT%" (GOTO ProcessFile)
GOTO END

:ProcessFile
IF "%DelExt%"=="*" (CALL :AA) ELSE (FOR %%i IN (%DelExt%) DO (call :AA %%i))
GOTO Print_Results

:AA
IF "%DelExt%"=="*" (SET DeleteExt=*) ELSE (SET DeleteExt=%1)
FOR /F "tokens=1* delims=" %%j IN ('DIR /A-H-D /B %SUB% %StartIn%\*.%DeleteExt%') do (
set FileName=%%j
call :Set_FileName
call :PROCESSFILE_Dext)
GOTO End

:ProcessFile_Sext
FOR %%i IN (%SaveExt%) DO (CALL :Hide_Files %%i)
CALL :AA *
FOR %%i IN (%SaveExt%) DO (CALL :Show_Files %%i)
GOTO Print_Results

:Hide_Files
SET SaveTheseExt=%1
attrib +H %StartIn%\*.%SaveTheseExt% %Sub%
IF EXIST zzz.txt (DEL /F zzz.txt)
DIR /aH /B %StartIn%\*.%SaveTheseExt% %Sub% > zzz.txt
FOR /f %%c IN (zzz.txt) DO (CALL :Skipped)
GOTO END

:Skipped
SET /A FChecked_Counter=%FChecked_Counter%+1
SET /A Total_Files=%Total_Files%+1
GOTO END

:Show_Files
SET SaveTheseExt=%1
attrib -H %StartIn%\*.%SaveTheseExt% %Sub%
GOTO END

:Set_FileName
SET Online=null
IF EXIST "%FileName%" (SET Online=Short)
IF "%Online%"=="Short" (
FOR /F "skip=5" %%k IN ('DIR "%FileName%"') DO (
SET Temp=%%k
GOTO END
))

IF EXIST "%StartIn%\%FileName%" (SET Online=Long)
IF "%Online%"=="Long" (
FOR /F "skip=5" %%k IN ('DIR "%StartIn%\%FileName%"') DO (
SET Temp=%%k
GOTO END
))

ECHO File Not Available: "%FileName%"
Goto END

:PROCESSFILE_Dext
IF "%Online%"=="null" (Goto End)
SET fyyyy=%temp:~6%
IF /I %fyyyy% LSS 100 SET fyyyy=20%fyyyy%
IF /I %fyyyy% GTR 2069 SET fyyyy=19%temp:~6%
SET fmm=%temp:~0,2%
SET fdd=%temp:~3,2%
IF /I "%DelRO%"=="Yes" (SET Force_Delete=/F)
IF /I "%Prog%"=="DIR" (SET Force_Delete=)
IF /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
IF /I %SubDir% NEQ YES (
IF EXIST "%StartIn%\%FileName%" (%PROG% %Force_Delete% "%StartIn%\%FileName%")
ECHO Removed..:"%StartIn%\%FileName%"
) ELSE (
ECHO Removed :"%FileName%"
IF EXIST "%FileName%" (%PROG% %Force_Delete% "%FileName%"))
SET /A Delete_Counter=%Delete_Counter%+1
) Else (SET /A FChecked_Counter=%FChecked_Counter%+1)
SET FileName=
SET temp=
SET fyyyy=
SET fmm=
SET fdd=
SET /A Total_Files=%Total_Files%+1
GOTO END

:Print_Results
ECHO.
ECHO The number of files deleted : %Delete_Counter%
ECHO.
ECHO The number of files passed over : %FChecked_Counter%
ECHO.
ECHO The Total number of files processed: %Total_Files%
ECHO.

REM Get Finish Time
REM This will load the current time into 'Finish' variables.
FOR /f "tokens=1-3 delims=:." %%a IN ('ECHO %time%') do (
SET Finish_hh=%%a
SET Finish_mm=%%b
SET Finish_ss=%%c
)
FOR /f "tokens=1-3 delims=: " %%a IN ('time /t') do (SET Finish_Meridian=%%c)

REM Calculate Time
REM Calculate the difference in time between Start and Finish times

IF %Start_ss% GTR %Finish_ss% (
SET /A Finish_ss=%Finish_ss%+60
SET /A Finish_mm=%Finish_mm%-1
)
SET /A Diff_ss=%Finish_ss%-%Start_ss%

IF %Start_mm% GTR %Finish_mm% (
SET /A Finish_mm=%Finish_mm%+60
SET /A Finish_hh=%Finish_hh%-1
)
SET /A Diff_mm=%Finish_mm%-%Start_mm%

IF %Start_hh% GTR %Finish_hh% (
SET /A Finish_hh=%Finish_hh%+24
SET Finish_day=1
)
SET /A Diff_hh=%Finish_hh%-%Start_hh%

IF %Diff_hh%==0 SET Diff_hh=00
IF %Diff_hh%==1 SET Diff_hh=01
IF %Diff_hh%==2 SET Diff_hh=02
IF %Diff_hh%==3 SET Diff_hh=03
IF %Diff_hh%==4 SET Diff_hh=04
IF %Diff_hh%==5 SET Diff_hh=05
IF %Diff_hh%==6 SET Diff_hh=06
IF %Diff_hh%==7 SET Diff_hh=07
IF %Diff_hh%==8 SET Diff_hh=08
IF %Diff_hh%==9 SET Diff_hh=09
IF %Diff_mm%==0 SET Diff_mm=00
IF %Diff_mm%==1 SET Diff_mm=01
IF %Diff_mm%==2 SET Diff_mm=02
IF %Diff_mm%==3 SET Diff_mm=03
IF %Diff_mm%==4 SET Diff_mm=04
IF %Diff_mm%==5 SET Diff_mm=05
IF %Diff_mm%==6 SET Diff_mm=06
IF %Diff_mm%==7 SET Diff_mm=07
IF %Diff_mm%==8 SET Diff_mm=08
IF %Diff_mm%==9 SET Diff_mm=09
IF %Diff_ss%==0 SET Diff_ss=00
IF %Diff_ss%==1 SET Diff_ss=01
IF %Diff_ss%==2 SET Diff_ss=02
IF %Diff_ss%==3 SET Diff_ss=03
IF %Diff_ss%==4 SET Diff_ss=04
IF %Diff_ss%==5 SET Diff_ss=05
IF %Diff_ss%==6 SET Diff_ss=06
IF %Diff_ss%==7 SET Diff_ss=07
IF %Diff_ss%==8 SET Diff_ss=08
IF %Diff_ss%==9 SET Diff_ss=09

REM Print Time
SET End_Time=%time%
ECHO.
ECHO The program started at: %Start_Time% %Start_Meridian%
ECHO The program ended at: %End_Time% %Finish_Meridian%
ECHO hh:mm:ss.mm
ECHO.
ECHO This is the amount of time elapsed from the
ECHO start to the end of the program.
ECHO.
ECHO %Diff_hh%:%Diff_mm%:%Diff_ss%
ECHO hh:mm:ss
ECHO.
ECHO. Number of Days Elapsed: %Finish_day%

:HouseCleaning
SET Check1=
SET Check2=
SET Check3=
SET Check4=
SET Check5=
SET Check=
SET Days=
SET DD=
SET DeleteExt=
SET Delete_Counter=
SET DelEXT=
SET DelRO=
SET DEXT=
SET Diff_hh=
SET Diff_mm=
SET Diff_ss=
SET End_Time=
SET EXT=
SET FChecked_Counter=
SET FDD=
SET FileName=
SET Finish_day=
SET Finish_hh=
SET Finish_Meridian=
SET Finish_mm=
SET Finish_ss=
SET FMM=
SET Force_Delete=
SET FYYYY=
SET Help_A=
SET Help_B=
SET MM=
SET Online=
SET Prog=
SET SaveEXT=
SET SaveTheseExt=
SET SEXT=
SET Space=
SET StartIn=
SET Start_hh=
SET Start_Meridian=
SET Start_mm=
SET Start_ss=
SET Start_Time=
SET Sub=
SET SubDir=
SET Sw1=
SET Sw2=
SET Sw3=
SET Sw4=
SET Sw5=
SET Sw6=
SET Sw7=
SET Sw8=
SET Sw9=
SET Total_Files=
SET TT=
SET XX=
SET YYYY=
IF EXIST zzz.txt (DEL /F zzz.txt)
GOTO END

:Error
Title Clean.bat - User Assistance
CLS
ECHO.
ECHO *******************************************
ECHO *********** Purge_Files.bat ***********
ECHO *********** Help Section ***********
ECHO *******************************************
ECHO.
ECHO Best viewed with a window width of: 105
ECHO.
ECHO Written by: Pete Soski
ECHO Last Revision Date: 18 February 2009
ECHO Contact: SoskiP@Yahoo.com
ECHO.
ECHO KNOWN ISSUES
ECHO #1: "The number of files passed over" is not accurate when using the
ECHO /DEXT option.
ECHO.
ECHO #2: Paths with SPACES will NOT work properly with the /PATH: option.
ECHO.
ECHO #3: DOS Limitation of 9 (space separated) switches
ECHO.
ECHO *******************************************
ECHO *************************************************
ECHO.
ECHO Command name: Purge_Files.bat
ECHO.
ECHO Switches: /DAY: /PATH: /DEXT: /SEXT: /SUB /ROD /TEST
ECHO.
ECHO The switches; as specified above, are also case insensitive.
ECHO The order of the switches does not matter.
ECHO It is unnecessary to use all the switches.
ECHO.
ECHO The minimum switches are: /DAY: and /PATH:
ECHO Using only these switches will imply the deletion of everything [*.*] in the specified
ECHO path; as qualified by older then /DAY: specification. Yes, THIS IS DANGEROUS!!!
ECHO.
ECHO The Purge_Files.bat program is not case sensitive.
ECHO Thus, running PURGE_FILES.bat or purge_files.bat incurs no changes.
ECHO The program will also operate correctly if "Purge_Files" is changed; the
ECHO ".bat" is critical, do not change the extension. For example Purge_Files.bat
ECHO can be changed to PF.bat, and the program will still run as designed.
ECHO.
ECHO Purge_Files.bat was written and tested on Microsoft Windows XP Pro SP2.
ECHO Other operating system may work correctly, but I have not tested them.
ECHO.
ECHO --------------
ECHO User Selected Switches - Descriptions:
ECHO --------------
ECHO.
ECHO /DAY:[number]
ECHO Specify the age (in days) of files that will be deleted;if the file is older.
ECHO Example: /DAY:10
ECHO.
ECHO /PATH:[drive letter and path]
ECHO This will be starting path to be searched for qualifying files. Note, there is not
ECHO an ending backslash [\]. Paths with SPACES will NOT WORK.
ECHO Example: /PATH:D:\FTP\Bob_The_User
ECHO Example: /path:d:\logfiles
ECHO.
ECHO /DEXT:[delete files of this extension type]
ECHO DEXT is the abbreviation for "DELETE EXTENSION"
ECHO Specify the extension(s) of the files to be deleted, separated by a comma [,]
ECHO Example: /DEXT:TMP :Would delete all (*.tmp) TMP files.
ECHO Example: /DEXT:TMP,LOG :Would delete all (*.tmp and *.log) TMP and LOG files.
ECHO Over 50 file extensions can be specified. Comma delimited only.
ECHO.
ECHO /SEXT:[save files of this extension type, DELETE ALL OTHERS]
ECHO SEXT is the abbreviation for "SAVE EXTENSION"
ECHO Specify the extension(s) of the files in which to keep; DELETING ALL OTHERS.
ECHO Over 50 file extensions can be specified. Comma delimited only.
ECHO Example: /SEXT:TMP :Would delete all files, except TMP files (*.tmp)
ECHO Example: /SEXT:TMP,LOG :Would delete all files, except TMP and LOG files (*.tmp and *.log)
ECHO.
ECHO /SUB
ECHO SUB; or Sub directory, is used when evaluation of files within sub directories is desired.
ECHO SUB has a starting path based on the value given with /PATH.
ECHO SUB can be used in conjunction with any other switch or switches.
ECHO.
ECHO /ROD
ECHO ROD; or Read-Only-Delete, is used when purging of Read-Only files is needed.
ECHO ROD can be used in conjunction with any other switch or switches.
ECHO.
ECHO /TEST
ECHO TEST; obviously, is used to test the Settings without destructively deleting the target files.
ECHO The closing file-counter will still process as if the purging took place.
ECHO The TEST switch changes the DELETE (DEL) command to a DIR(ectory) command.
ECHO The TEST switch can be run with any other switch or switches.
ECHO.
ECHO Technology Under-The-Hood
ECHO In brief, when using the /SEXT: switch, the files selected for saving are given
ECHO the attribute of [hidden]. Then the; remaining, viewable files are swept and purged. Finally,
ECHO the Saved files are un-hidden. This can be an issue if your local environmental operation
ECHO depends on certain files from being viewed. When un-hiding files this program will remove
ECHO the hidden attribute from any file;based on extension cited in /SEXT and in the /PATH:.
ECHO If desired, I can change the program to use the Achive Bit, instead of the Hidden Bit.
ECHO.
ECHO **********************************************************************************************
ECHO Example #1:
ECHO Purge_Files.bat /DAY:8 /PATH:C:\Temp /SEXT:EXE,BAT,DLL
ECHO.
ECHO This example will delete all files from:
ECHO - C:\Temp
ECHO - Delete all files (over 8 days)
ECHO - Exceptions *.EXE, *.BAT, or *.DLL.
ECHO.
ECHO Example #2:
ECHO purge_files /DEXT:LOG,TMP /DAY:365 /PATH:D:\Logs_Directory /ROD
ECHO.
ECHO This example will delete all files from:
ECHO - D:\Logs_Directory
ECHO - Delete files, with only LOG or TMP extensions.
ECHO - Which are over 365 days old
ECHO.
ECHO **********************************************************************************************
ECHO.
pause
Call :HouseCleaning
GOTO END

:END


See More: Delete Files Older Then x-Days v.3

Report •


#1
February 18, 2009 at 14:53:40
Ok, thanks.

"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10


Report •
Related Solutions


Ask Question