VBS to delete file time in folder and sub folders

I'm looking to have a scheduled task that runs a VBS script (called from a bat file) that deletes PDF Files is a folder and it's sub folders. Thank you in advance for your help!!!!!
LVL 9
mystics7Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mystics7Connect With a Mentor Author Commented:
here is the code if anyone needs it. This will delete specified files older than specified time and in subfolders
OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO, MaxAge, IncludeSubFolders
' Folder to delete files
strFolder = "C:\Test"
' Delete files from sub-folders?
includeSubfolders = true
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "pdf"
' Max File Age (in Days).  Files older than this will be deleted.
maxAge = 30

' ************************************************************

set objFSO = createobject("Scripting.FileSystemObject")

DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

wscript.echo "Finished"

sub DeleteFiles(byval strDirectory,byval strExtensionsToDelete,byval maxAge,includeSubFolders)
	DIM objFolder, objSubFolder, objFile
	DIM strExt

	set objFolder = objFSO.GetFolder(strDirectory)
	for each objFile in objFolder.Files
		for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
			if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
				IF objFile.DateLastModified < (Now - MaxAge) THEN
					wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified 
					objFile.Delete
					exit for
				END IF
			end if
		next
	next	
	if includeSubFolders = true then ' Recursive delete
		for each objSubFolder in objFolder.SubFolders
			DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubFolders
		next
	end if
end sub

Open in new window

0
 
rettiseertCommented:
If you are using a bat file why not to use a simple shell command? try this in the bat:

DEL C:\your_folder\*.pdf /S /Q /F
0
 
mystics7Author Commented:
this will also delete the pdfs from the sub folders?
0
 
mystics7Author Commented:
I also forgot to mention that this has to be done one files older than 30 days
0
 
Bill PrewCommented:
Why not just use this handy little free utility called DELAGE32

http://home.mnet-online.de/horst.muc/win/delage.htm
http://home.mnet-online.de/horst.muc/wbat32.htm

So a simple command like the following could do it for you:

delage32 c:\temp\*.pdf 31 /recurse

~bp
0
All Courses

From novice to tech pro — start learning today.