Solved

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

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

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 53

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 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