Solved

delete files more then 30 days old

Posted on 2008-10-01
10
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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