Delete files older than 30 days by created date not modified...

Posted on 2009-02-10
Last Modified: 2012-06-27
Hi all,

I'm trying to put together a script to delete any files from a shared area that have been created more than 30 days ago. I have used the below scripts in a batch file from robocopy and from forfiles but neither seems to do what I want.

FORFILES /p c:\temp /s /m *.* /d -10 /c "cmd /c del /q @path"


"C:\WINDOWS\system32\ROBOCOPY.EXE" c:\test\ c:\test\old  /move /sec /e /XO /r:1 /w:1 /minage:30

The problem is that it deletes files older than 30 days when looking at the modified date not the created date. The idea is that the shared area is for transferring files from one server to another. Thus the created date is different to the Modified date.

Is there a way to delete files older than 30 days from the created date? See 'A' in attachment and not 'B' the modified date


Question by:ranski
    LVL 16

    Accepted Solution

    LVL 6

    Assisted Solution


    ' DelBasedOnCreationDate.vbs - delete files created over 30 days ago based on Date Created
    Set fs = CreateObject("Scripting.FileSystemObject")
    for each f in fs.getfolder(".").Files
         if Datediff("d",date,f.DateCreated) < -30 then
               wscript.echo f.DateCreated & "     " &
        end if
    This vbs script will echo the results to your screen.  Once you are sure the files you want to delete are being echoed out properly, uncomment the f.delete line.

    LVL 6

    Expert Comment

    The way the above code is written, you need to be in the directory you want to delete files from when you execute it.  You will probably want to change the getfolder(".") to getfolder("c:\temp").
    Another question:  Do you really want to delete the files or do you want to move them?
    LVL 21

    Assisted Solution

    This should do it if your date format is MM/dd/yyyy or M/d/yyyy

    If not then we'll need to move some variables around.
    @echo off
    set Folder=c:\test
    set OldFolder=c:\test\old
    call :SubDays 30
    for /f "tokens=1,2,3,6,* delims=/ " %%a in ('dir /a-d /tc "%Folder%"') do (
        if %%c%%a%%b leq Year%%Month%%Day% ECHO move "%Folder%\%%e" "%OldFolder%"
    exit /b
    REM -------- Thanks to AmazingTech --------------
    Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
    Set Year=%date:~-4%
    Set Day=1%date:~-7,2%
    Set Month=1%date:~-10,2%
    Set /a Day-=%1
    If !Day! LEQ 100 (
        Set /a Month-=1
        if !Month! == 100 (
            Set /a Year-=1
            Set Month=112
        Set MCounter=101
        Set DaysInThisMonth=
        for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !Month! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
        if !Month! == 102 (
            if %Year:~-2% == 00 (
                Set /a Mod=!Year:~0,2! %% 4
                if !Mod! == 0 Set /a DaysInThisMonth+=1
            ) else (
                Set /a Mod=!Year! %% 4
                if !Mod! == 0 Set /a DaysInThisMonth+=1
        Set /a Day+=!DaysInThisMonth!

    Open in new window


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 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

    17 Experts available now in Live!

    Get 1:1 Help Now