Solved

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

Posted on 2011-02-22
9
706 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
 
LVL 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

943 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

4 Experts available now in Live!

Get 1:1 Help Now