Solved

delete files more then 30 days old

Posted on 2008-10-01
10
520 Views
Last Modified: 2011-10-19
Hi,
I have an application that creates a txt and stores it on the hard drive.
These files are only needed if an error acures.
How do I use a bat or cmd file to delete files more then 30 days old?
thanks
Al
0
Comment
Question by:lacroix_al
  • 5
  • 4
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22615839
This will work for you:
cleanup.vbs
Dim objFSO As Object

Dim objOutputFile As Object

Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFolder = "C:\Testing"

Set objOutputFile = objFSO.OpenTextFile("C:\FilesRemoved.log", ForAppending)
 

With objOutputFile

  .WriteLine "==========================="

  .WriteLine "Removal summary for " & Date

  .WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=-="

  .WriteLine

End With
 

'intDel is the number of days old you want to check for.  I set it to 1 for testing.

intDel = 30

dtOld = DateAdd("d", -intDel, Date)
 

Set objFolders = objFSO.GetFolder(strSourceFolder)

'Get the files in the current folder
 

For Each fil In objFolders.Files

  'If a file exists that hasn't been accessed in intDel days or more, delete it

  If DateDiff("d", fil.datelastaccessed, dtOld) >= intDel Then

    objOutputFile.WriteLine fil.Path & " was removed at " & Now

    objFSO.DeleteFile fil.Path

  End If

Next
 

With objOutputFile

  .WriteLine

  .WriteLine "Process completed at " & Now

  .WriteLine "==========================="

  .Close

End With
 

Set objOutputFile = Nothing

Set objFSO = Nothing

wscript.quit

Open in new window

0
 

Author Comment

by:lacroix_al
ID: 22616032
Thank you for the reply.
Please explain how to use this file?
AL
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22616059
Save it as cleanup.vbs and either double-click it, or run it from a command shell/task scheduler using
cscript C:\pathtofile\cleanup.vbs

You'll need to alter strSourceFolder line to point to the folder you want to clean (currently C:\Testing)
0
 
LVL 63

Expert Comment

by:SysExpert
ID: 22616095
also see the small delen program that is designed to do this in batch files and includes examples.

http://www.geocities.com/jadoxa/delenxrd/index.html


I hope this helps !
0
 

Author Comment

by:lacroix_al
ID: 22616236
sirbounty
I created a folder testing on c:\
filed it with some old txt files
copied the above code to a txt file and named it cleanup.vbs
saved it to c:\
double clicked on it and got an error (see attached)

What do I do to fix this error?
Thanks
Al

error.doc
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 67

Expert Comment

by:sirbounty
ID: 22616283
Whoops - sorry about that - try this version:
Const ForAppending = 8

strSourceFolder = "C:\Testing"

strLog = "C:\FilesRemoved.Log"
 

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objOutputFile : Set objOutputFile = objFSO.CreateTextFile(Replace(strLog, ".Log", Month(Date) & Day(Date) & Year(Date) & "_.Log"))

 

With objOutputFile

  .WriteLine "==========================="

  .WriteLine "Removal summary for " & Date

  .WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=-="

  .WriteLine

End With

 

'intDel is the number of days old you want to check for.  I set it to 1 for testing.

intDel = 30

dtOld = DateAdd("d", -intDel, Date)

 

Set objFolders = objFSO.GetFolder(strSourceFolder)

'Get the files in the current folder

 

For Each fil In objFolders.Files

  'If a file exists that hasn't been accessed in intDel days or more, delete it

  If DateDiff("d", fil.datelastaccessed, dtOld) >= intDel Then

    objOutputFile.WriteLine fil.Path & " was removed at " & Now

    objFSO.DeleteFile fil.Path

  End If

Next

 

With objOutputFile

  .WriteLine

  .WriteLine "Process completed at " & Now

  .WriteLine "==========================="

  .Close

End With

 

Set objOutputFile = Nothing

Set objFSO = Nothing

wscript.quit

Open in new window

0
 

Author Comment

by:lacroix_al
ID: 22616375
ok
I resaved this code
ran it by double clicking
it created the log file but didn't remove any files.
I changed the intdate to = 1 instead of 30
it still didn't delete anything.

How do I fix this?
Thanks
Al
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 22616406
Well, it's designed to delete files that weren't accessed in over 30 days (or x number of days).
Are you looking for creation date perhaps?

Change this line, if so:
  If DateDiff("d", fil.datelastaccessed, dtOld) >= intDel Then

to read:
  If DateDiff("d", fil.DateCreated, dtOld) >= intDel Then
0
 

Author Comment

by:lacroix_al
ID: 22616471
yes that was the problem
Thank you for all your help
Al
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22616483
Happy to help - thanx for the grade! :^)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

23 Experts available now in Live!

Get 1:1 Help Now