Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBscript for auto file deletion including subfolders

Posted on 2006-11-24
6
Medium Priority
?
7,046 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 2000 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
Technology Partners: 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

660 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