Solved

Script to delete all subfolders except newest?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automate PC's, Laptop to correct OU 4 45
Can VBS count the number of items in an array 8 54
Add a second combobox VBA Excel 8 34
Powershell Split 18 29
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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