Solved

Script to delete all subfolders except newest?

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

724 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