1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Batch Script to delete files older than specified date

Discussion in 'Programming' started by Cleptography, Sep 6, 2010.

  1. Cleptography

    Cleptography New Member

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

    Batch to delete files older than specified date
    @echo off
    :                                       :
    : 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
    echo.Usage:   %0 [Date of Files]
    echo.Example: %0 %date:~4,2%/%date:~7,2%/%date:~10,4%
    echo.Deletes: Files Created On Or Before %date:~4,2%/%date:~7,2%/%date:~10,4%
  2. gregbaldwen

    gregbaldwen New Member

    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.
    Cleptography likes this.
  3. kenprinceton

    kenprinceton New Member

    The code is quiet helpful in my internet T1 Price site project thanks for it.
  4. shabbir

    shabbir Administrator Staff Member

Share This Page