• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6071
  • Last Modified:

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

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
0
ranski
Asked:
ranski
  • 2
3 Solutions
 
Jammer59Commented:

' 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.

0
 
Jammer59Commented:
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?
0
 
AmazingTechCommented:
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

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now