?
Solved

VBS: Delete any files that are > 5 days old ANDF 0 kb filesize

Posted on 2009-05-03
5
Medium Priority
?
669 Views
Last Modified: 2012-05-06
Hello, I need a vbs that will scan a folder and delete any files that are > 5 days old with 0 kb filesize. Can anyone offer any suggestions?
Dim fso, objShell
'===================================
'Start of Sub-routine
Sub fFIND(oFolder)
	DIM oFile
	DIM oSdate, oCdate,objFile
 
	'Look at each file in the current folder
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.GetFile((oFile.Name), "C:\myfolder\Log20*")
	
	For Each oFile in oFolder.Files
		
		'Check for a file of the format *LOG20*.TXT
		if inStr(uCase(oFile.Name), "LOG20") > 0 and uCase(right(oFile.name,4)) = ".TXT" then 
			oSdate = DateValue(oFile.DateLastModified)
			oCdate = DateDiff("d", oSdate, Date)
												
			'Log and delete *LOG20*.TXT files older than 15 days
	if oCdate >= 15 And objFSO.size < 2 Then
			Call LoggerF(oFile.Path)
		     	oFile.Delete True
			End if		
		
		end If
 
	Next
 
End Sub
'======================================
 
'=====================
'Write Log Sub
 
 
Sub LoggerF(fPath)
	DIM oFSO, oStream, oFile, WshNetwork
 
	On Error Resume Next
 
	Set WshNetwork = WScript.CreateObject("WScript.Network")
 
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	Set oFile = oFSO.GetFile("CleanupLog.txt")
	Set oStream = oFile.OpenAsTextStream(8, 0)
 
	oStream.Write VbCrLf & Ucase(WshNetwork.UserName) & " deleted " & fPath & " at " & Now
	oStream.Close
	
	set oStream = Nothing
	set oFile = Nothing
	set oFSO = Nothing
	set WshNetwork = Nothing
 
End Sub
'=====================
 
on error resume Next
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
 
'Call the Sub-routine that searches and deletes the files
fFIND fso.GetFolder("C:\myfolder")
objShell.LogEvent EVENT_SUCCESS, "Log file cleanup finished at " & date
Wscript.Quit
set objShell = Nothing
set fso = Nothing

Open in new window

0
Comment
Question by:MonMuscRS
[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
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:TakedaT
ID: 24291888
Is there something wrong in the code you provided?  Do you get errors when its ran?  If so, can you tell what lines the errors are on?
0
 

Author Comment

by:MonMuscRS
ID: 24291909
No errors, no results. The files are not being eliminated.
 
0
 
LVL 7

Expert Comment

by:Alpha Au
ID: 24292375
please remark the
on error resume Next
and run again. (for debug)

all error are hidden so that you found no error.
0
 

Author Comment

by:MonMuscRS
ID: 24295323
No error still when I commented out the "On Error Resume Next" but when I commented out the file size dependent logic, it worked. I really would like to only remove the 0 kb files, can you see a flaw in my logic?
Thank you for your time!
0
 
LVL 10

Accepted Solution

by:
TakedaT earned 500 total points
ID: 24300346
If this is where your file size logic is:
      if oCdate >= 15 And objFSO.size < 2 Then

Shouldnt it read
      if oCdate >= 15 And oFile.size < 2 Then
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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