Solved

Script to delete all subfolders except newest?

Posted on 2012-03-30
3
726 Views
Last Modified: 2012-06-27
We have an application that is meant to create a folder with files in it daily, but when it creates todays folder, it's meant to delete previous days folders. However this isn't happening, so as a short term fix I'd like to write a script that will run daily, and delete all previous folders except the latest. I have found the below script online, which works perfectly for files - if I have 3 files each 1 minute apart, it will delete the 2 oldest, and keep the newest - however I'm having some problems getting it working for folders. Can anyone help me modify the script, so all subfolders of 'd:\temp' are deleted except the latest?

Const adVarChar = 200
Const adDate = 7

ipreserve=1    '# of files to preserve
sfolderpath="d:\temp"    'your path to the folder

set fso=createobject("scripting.filesystemobject")
if not fso.folderexists(sfolderpath) then
    set fso=nothing
    wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted."
    wscript.quit
end if

set ofolder=fso.getfolder(sfolderpath)
set rs = createobject("ador.recordset")
with rs.fields
    .append "filepath",adVarChar,255
    .append "datelastmodified",adDate
end with

with rs
    .open
    for each ofile in ofolder.files
        .addnew array("filepath","datelastmodified"), array(ofile.path,ofile.datelastmodified)
        .update
    next
end with

icount=0
if not (rs.eof and rs.bof) then
    rs.sort="datelastmodified desc"
    rs.movefirst
    do while not rs.eof
        icount=icount+1
        if icount>ipreserve then
            on error resume next
            fso.deletefile rs.fields("filepath"),true
            on error goto 0
        end if
        rs.movenext
    loop
end if

set rs=nothing
set ofolder=nothing
set fso=nothing

Open in new window

0
Comment
Question by:bjblackmore
3 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37786984
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 37787332
Try the following..

Const adVarChar = 200
Const adDate = 7

ipreserve=1    '# of files to preserve
sfolderpath="d:\temp"    'your path to the folder

set fso=createobject("scripting.filesystemobject")
if not fso.folderexists(sfolderpath) then
    set fso=nothing
    wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted."
    wscript.quit
end if

set ofolder=fso.getfolder(sfolderpath)
set rs = createobject("ador.recordset")
with rs.fields
    .append "subfolderpath",adVarChar,255
    .append "datelastmodified",adDate
end with

with rs
    .open
    for each osf in ofolder.subfolders
        .addnew array("subfolderpath","datelastmodified"), array(osf.path,osf.datelastmodified)
        .update
    next
end with

icount=0
if not (rs.eof and rs.bof) then
    rs.sort="datelastmodified desc"
    rs.movefirst
    do while not rs.eof
        icount=icount+1
        if icount>ipreserve then
            on error resume next
            fso.deletefolder rs.fields("subfolderpath"),true
            on error goto 0
        end if
        rs.movenext
    loop
end if

set rs=nothing
set ofolder=nothing
set fso=nothing
                                  

Open in new window

0
 

Author Closing Comment

by:bjblackmore
ID: 37787752
Works perfectly!
0

Featured Post

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.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

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