Solved

VBscript for auto file deletion including subfolders

Posted on 2006-11-24
6
7,033 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

752 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