Solved

delete files more then 30 days old

Posted on 2008-10-01
10
541 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Simple Linear Regression

729 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