Script To Delete Certain File Types in a Certain Location (Recursively) that is One Day Old

Please help! Either in batch script, powershell or vbscript.Thank you!
mmoyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmoyaAuthor Commented:
Is this safe and good?

OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO

' ************************************************************
' Setup
' ************************************************************

' Folder to delete files from (files will also be deleted from subfolders)
strFolder = "C:\MyFolderLocation"
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "wav,avi,mp3,pdf"

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

set objFSO = createobject("Scripting.FileSystemObject")

RecursiveDeleteByExtension strFolder,strExtensionsToDelete

wscript.echo "Finished"

sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
      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
                        wscript.echo "Deleting:" & objFile.Path
                        objFile.Delete
                        exit for
                  end if
            next
      next      
      for each objSubFolder in objFolder.SubFolders
            RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
      next
end sub
0
mmoyaAuthor Commented:
I need a code insert wherein the age of the file will also be considered.....Thank you!
0
prashanthdCommented:
Please try this code, age of file is based on datelastmodified, it can be changed to datecreated as per requirement

OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO, MaxAge, IncludeSubFolders

' ************************************************************
' Setup
' ************************************************************

' 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 = "tmp,temp"
' Max File Age (in Days).  Files older than this will be deleted.
maxAge = 1

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

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
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bill PrewCommented:
I use a great little utility for this called DELAGE32 located at:

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


~bp
0
mmoyaAuthor Commented:
@ prashanthd, it works great. Just have to add "end sub" at the end and it worked fine.

Also, is there a way to create a log of all the files (with timestamps) that were deleted from a folder. I'm trying to do it using the batch file, such as C:\>cscript C:\Users\mmoya627\Documents\Scripts\CleanUpFilesV30223
11.vbs -q > (what should be the string for this??).

0
prashanthdCommented:
You can use the following code in batch file
A log file(cleanup.log) will be created and appended each time the script runs.

echo C:\Users\mmoya627\Documents\Scripts\
cscript CleanUpFilesV3022311.vbs >> Cleanup.log

0
mmoyaAuthor Commented:
@prashanthd:- thank you! last question - I have to schedule this script to run daily and if I do, the output will be overwritten everytime I need the script runs the next day. Is there a way I can attach a timestamp on the cleanup.log output file so it doesnt get overwritten everytime the script has to run?
0
prashanthdCommented:
">> Cleanup.log"  command - appends each time and does not overwrite.

Below code can be used to write to a new file every day with date

echo C:\Users\mmoya627\Documents\Scripts\
cscript CleanUpFilesV3022311.vbs > %date:~4,2%%date:~7,2%%date:~10,4%_cleanup.log

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmoyaAuthor Commented:
@prashanthd: - quick question - if i want to convert the script to non file extension specific, should I just omit this line? ->

 ' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "tmp,temp"

Thanks for your help. Appreciate it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.