Link to home
Start Free TrialLog in
Avatar of chekfu
chekfu

asked on

VBscript for auto file deletion including subfolders

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!
Avatar of killbrad
killbrad
Flag of United States of America image

' 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

Avatar of chekfu
chekfu

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chekfu

ASKER

it's 30 days
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.
Avatar of chekfu

ASKER

I prefer vbscript from Idle_Mind. Thanks guys!!!