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

Posted on 2009-05-03
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(,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


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



	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


set objShell = Nothing

set fso = Nothing

Open in new window

Question by:MonMuscRS
  • 2
  • 2
LVL 10

Expert Comment

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?

Author Comment

ID: 24291909
No errors, no results. The files are not being eliminated.

Expert Comment

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

all error are hidden so that you found no error.

Author Comment

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!
LVL 10

Accepted Solution

TakedaT earned 125 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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

20 Experts available now in Live!

Get 1:1 Help Now