Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/articles/programming/)
-   -   Batch Script to delete files older than specified date (http://www.go4expert.com/articles/batch-script-delete-files-specified-date-t23211/)

Cleptography 6Sep2010 07:20

Batch Script to delete files older than specified date
 
Batch to delete files in a directory older than the specified date

Batch to delete files older than specified date
-----------------------------------------------------
Code:

@echo off
 setlocal

:---------------------------------------:
:                                      :
: Delete Files In The Current Directory :
:    Older Than The Specified Date    :
:                                      :
:---------------------------------------:

:: Set delete date
set _FileDate=%1

:: Check delete date arg if it does not exist goto :Usage
if not defined _FileDate goto Usage

:: Parse and set user date REM This is the date used to compare
for /f "tokens=1,2,3 delims=/" %%a in ("%_FileDate%") do (
        set "_Month=%%a"
                set "_Day=%%b"
                        set "_Year=%%c"
)
:: Parse month and day of files and delete files older than date
for %%d in (*) do (
        for /f "tokens=1,2,3,6* delims=/ " %%e in ("%%~tnxd") do (
                if %%e leq %_Month% (
                        if %%f leq %_Day% (
                                if %%g leq %_Year% (
                                        del /f /q %%h
                                )
                        )
                )
        )
)
goto :eof

:Usage
echo.Usage:  %0 [Date of Files]
echo.
echo.Example: %0 %date:~4,2%/%date:~7,2%/%date:~10,4%
echo.
echo.Deletes: Files Created On Or Before %date:~4,2%/%date:~7,2%/%date:~10,4%


gregbaldwen 6Sep2010 09:18

Re: Batch Script to delete files older than specified date
 
Indeed a good recommendation, anyway thanks for the code presented seems to be helpful in my site development for T1 Internet Service site project. Definitely a good resource.

kenprinceton 8Sep2010 09:02

Re: Batch Script to delete files older than specified date
 
The code is quiet helpful in my internet T1 Price site project thanks for it.

shabbir 4Oct2010 10:21

Re: Batch Script to delete files older than specified date
 
If you liked this article do nominate this article for Article of the month - Sep 2010 Before 15th of October 2010.


All times are GMT +5.5. The time now is 10:52.