Solved

delete files more then 30 days old

Posted on 2008-10-01
10
527 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

770 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