Solved

Script to delete all subfolders except newest?

Posted on 2012-03-30
3
725 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now