Solved

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

Posted on 2011-02-22
9
709 Views
Last Modified: 2012-05-11
Please help! Either in batch script, powershell or vbscript.Thank you!
0
Comment
Question by:mmoya
[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
  • 5
  • 3
9 Comments
 

Author Comment

by:mmoya
ID: 34952114
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
 

Author Comment

by:mmoya
ID: 34952155
I need a code insert wherein the age of the file will also be considered.....Thank you!
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 34953504
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 55

Expert Comment

by:Bill Prew
ID: 34953801
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
 

Author Comment

by:mmoya
ID: 34960984
@ 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
 
LVL 12

Expert Comment

by:prashanthd
ID: 34961955
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
 

Author Comment

by:mmoya
ID: 35000897
@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
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35003605
">> 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
 

Author Comment

by:mmoya
ID: 35353144
@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

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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…
Six Sigma Control Plans

717 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