Solved

VBscript for auto file deletion including subfolders

Posted on 2006-11-24
6
7,017 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
ID: 18007900
' 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
ID: 18008204
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
ID: 18008359
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
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.

 
LVL 1

Author Comment

by:chekfu
ID: 18008431
it's 30 days
0
 
LVL 7

Expert Comment

by:killbrad
ID: 18009169
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
ID: 18010230
I prefer vbscript from Idle_Mind. Thanks guys!!!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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 …

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