Solved

delete files more then 30 days old

Posted on 2008-10-01
10
515 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
Comment Utility
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
Comment Utility
Thank you for the reply.
Please explain how to use this file?
AL
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yes that was the problem
Thank you for all your help
Al
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Happy to help - thanx for the grade! :^)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
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…

743 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

8 Experts available now in Live!

Get 1:1 Help Now