We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

Medium Priority
6,898 Views
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"

and

"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

thanks

screenshot1.JPG
Comment
Watch Question

Tech Support professional
CERTIFIED EXPERT
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:

' 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 & "     " & f.name
           'f.delete
    end if
next
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.

Commented:
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?
Top Expert 2009
Commented:
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
SETLOCAL ENABLEDELAYEDEXPANSION
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 --------------
:SubDays
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.