Solved

VBscript for auto file deletion including subfolders

Posted on 2006-11-24
6
6,996 Views
Last Modified: 2013-11-18
Hi Experts

I'm looking for a working VBscript whereby it allows do a search on the specified folder D:\General. This folder contains multiple levels of subfolders.

I like to have files clean up every 30 days including files inside subfolders.

Please help! Thanks in advanced!
0
Comment
Question by:chekfu
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:killbrad
Comment Utility
' Objective: To delete old files from a given folder and all subfolders below
' Created by: MAK [ mak_999(AT)yahoo.com ]  on June 21, 2005
' Usage:
' cscript deloldfiles.vbs c:\dba\log 3
'      : It deletes files older than 3 days in the c:\dba\log folder
' cscript deloldfiles.vbs d:\general 30
'      :  deletes files older than 30 days from d:\general and all subfolders

Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
  set subfolders = folders.subfolders
  set files = folders.files
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
    file.delete
    end if
   
  next  

  for each folder in subfolders
    recurse folder
  next  

  set subfolders = nothing
  set files = nothing

end sub

0
 
LVL 1

Author Comment

by:chekfu
Comment Utility
I saved above script as file.vbs and save it in C:\General

Sorry, it doesn't work. It prompts:
Script: C:\General\file.vbs
Line: 10
Char: 1
Error: Subscript out of range
Code: 800A0009
Source: Microsoft VBScript runtime error
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Do you want to delete ALL files? ...or files older than 30 days?

It's not clear from your question...

Here is another version that deletes files older than a specific time period:

Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\General\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -30, Date)  ' 30 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
         fso.DeleteFile(file.Path)
      End If
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
End Function
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:chekfu
Comment Utility
it's 30 days
0
 
LVL 7

Expert Comment

by:killbrad
Comment Utility
chekfu:
You must provide arguments to the program, as indicated.
if you saved the script as file.vbs in c:\General, then you must run this from the command line:

cscript c:\General\file.vbs c:\general 30


However, keep in mind that if you have the script in the same folder you are running it against, if the script is older than the second argument you pass, you risk deleting the script itself.

It would not be much work to modify the script to allow you to choose the folder and number of days through a GUI, but that is just icing.. the script is fully functional, and produces a nice output as well.
0
 
LVL 1

Author Comment

by:chekfu
Comment Utility
I prefer vbscript from Idle_Mind. Thanks guys!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now