Solved

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

Posted on 2011-02-22
9
705 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 51

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

21 Experts available now in Live!

Get 1:1 Help Now