How to delete old files from a folder.

Posted on 2011-04-18
Last Modified: 2012-06-27
I'd like to request a batch file that will periodically run and delete all jpg, tiff, png files older thatn x days from a folder (and it's subfolders). Can this be done with no risk to files that do not meet the criterion? Folder is getting huge with old unnecessary files, but deleting 1 by 1 would be a monumental task. Environment is Windows Server 2003.
Thank you for your advice in this.
Question by:HardwareDude
    LVL 17

    Expert Comment

    by:Sikhumbuzo Ntsada
    I am not a batch file geek, i found this on my old folder, hope someone might help you twick it to your needs.

    @echo off
    for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
    set mm=%thedate:~0,2%
    set dd=%thedate:~3,2%
    set yyyy=%thedate:~6,4%
    set /A dd=%dd% - %OLDERTHAN%
    set /A mm=%mm% + 0
    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
    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
    goto ERROR
    set /A dd=31 + %dd%
    goto DONE
    set /A dd=30 + %dd%
    goto DONE
    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
    set /A dd=28 + %dd%
    goto DONE
    set /A dd=29 + %dd%
    if /i %dd% LSS 10 set dd=0%dd%
    if /I %mm% LSS 10 set mm=0%mm%
    for %%i in (*.*) do (
    set FileName=%%i
    call :PROCESSFILE %%~ti
    set mm=
    set yyyy=
    set dd=
    set thedate=
    goto EXIT
    ECHO   Where X is the number of days previous to Today.
    ECHO EX: "DELOLD 5" Deletes files older than 5 days.
    set temp=%1
    set fyyyy=20%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    :: +*************************************+
    :: | This is where the files are deleted |
    :: | Change the ECHO command to DEL to   |
    :: | delete. ECHO is used for test.      |
    :: +*************************************+
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
    ECHO %FileName%
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    :: ----------END-DELOLD.BAT-------------

    Open in new window

    LVL 13

    Accepted Solution

    You can do this with the forfiles command (I think it's built in with 2003 and higher, may need to download the resource kit if not)
    The command you would use would be something like this:

    forfiles /p "c:\folder" /s /m *.jpg /c "cmd /c del @file" /d -90

    That command would delete all .jpg files in the folder over 90 days old. You can modify the path, file type, and how old to delete as needed.
    LVL 13

    Expert Comment

    LVL 10

    Expert Comment

    I just tried your command line, and it works fantastic. Thanks

    I did not test your batch file however, Because environment settings may be different, from within your script, I'd adapt the following to your script then CALL it.
    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    	IF %%A GTR 0 (
    	SET DayVal=%%A
    	SET HourVal=%%B
    	SET MinVal=%%C
    	SET MonthVal=%%D
    	SET SecVal=%%E
    	SET YearVal=%%F
    EXIT /b

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    Not many admins are aware that GPOs can be activated and deactivated time-based. Time to change that :)
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now