1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Batch Script to delete files older than specified date

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

  1. Cleptography

    Cleptography New Member

    Joined:
    Sep 2, 2010
    Messages:
    39
    Likes Received:
    7
    Trophy Points:
    0
    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%
     
  2. gregbaldwen

    gregbaldwen New Member

    Joined:
    Sep 6, 2010
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    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

    Joined:
    Sep 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The code is quiet helpful in my internet T1 Price site project thanks for it.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83

Share This Page