# Delete a range of files?

**URL:** https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82
**Category:** Discussion
**Created:** [May 2, 2006, 10:33am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82 "2006-05-02T10:33:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![steve.ng](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@steve.ng](https://www.finalbuilder.com/forums/u/steve.ng)
#### Post date: [May 2, 2006, 10:33am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/1 "2006-05-02T10:33:03Z")

</div>

I’d like to have a set (or series of steps) that iterates through a series of files in a directory that match a certain filespec. For those files, I want to delete a bunch of them based on some “age” criteria:  
  
1. Keep only the N newest files and delete the rest.  
2. If that’s not possible, then delete anything older than N days.  
  
As I look through the actions, I see options for iterating files but I don’t see any ability to do any sort of date comparison or checking. I also didn’t see where I could specify the order for iterating, which would let me avoid date checking altogether (e.g. something like ordering with oldest first and deleting while the count of files is greater than N).  
  
Essentially, I’m looking for the best way to clean up a certain directory and remove yet another “occasional and manual” step from the process.  
  
Thanks,  
  
jd

---

<div class="post-metadata">

### Author: ![tate](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@tate](https://www.finalbuilder.com/forums/u/tate)
#### Post date: [May 4, 2006, 2:30am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/2 "2006-05-04T02:30:24Z")

</div>

Hi Jeff,  
  
The best way to do this is to define a FileSet (using the FileSet Define action). A fileset can select files using a variety of include and exclude patterns and filters (including date). Then you can pass this FileSet to the FileSet Delete Files action which will delete the selected files. The FileSet Delete Files action was added in build 214, here’s the download:  
  
  
[https://www.finalbuilder.com/downloads/finalbuilder/410/FB410\_214.exe](https://www.finalbuilder.com/downloads/finalbuilder/410/FB410_214.exe)

---

<div class="post-metadata">

### Author: ![steve.ng](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@steve.ng](https://www.finalbuilder.com/forums/u/steve.ng)
#### Post date: [May 8, 2006, 6:05am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/3 "2006-05-08T06:05:45Z")

</div>

Thanks, Tate… I’m downloading it now and will post if I have any issues with it. I think the main thing will be for the pattern/filter stuff that uses date to be dynamic (so that I always get “older than 10 days” or similar).  
  
Thanks again.

---

<div class="post-metadata">

### Author: ![steve.ng](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@steve.ng](https://www.finalbuilder.com/forums/u/steve.ng)
#### Post date: [May 8, 2006, 7:14am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/4 "2006-05-08T07:14:02Z")

</div>

Ok… so I downloaded that build and tinkered with it. Unfortunately, I don’t see the way to define the fileset I need. Basically, I’m trying to delete all files (logs) in a directory that are older than a certain date.  
  
In the Filters for defining a fileset, Date/Time is an option and I can specify an FB variable in there. The thing is that I don’t see a way to get it to do some date “math”.   
  
For example, I have an FB variable that is the build’s start date/time. So I can get a fileset that says “all files dated BEFORE this variable”, but that will delete all the logs except the current one. What I’d really like is “all files dated BEFORE (variable-10 days).”.  
  
I see two ways to go about it, but I don’t see where the fileset option lets me accomplish either:  
  
1. Get a fileset containing all files older than (current date - 10 days). Once I have it, delete the files in the fileset.  
  
2. Get a fileset containing all files, but have them ordered by date. Then I could try iterating the fileset and check each file’s date. If it’s old, I delete it.  
  
I prefer #1 and it seems that new build gets me really close… it’s just getting the variable it uses to be something other than the current date/time is the trick.  
  
Any tips, steps, or examples you can provide are greatly appreciated.  
  
Jeff

---

<div class="post-metadata">

### Author: ![tate](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@tate](https://www.finalbuilder.com/forums/u/tate)
#### Post date: [May 9, 2006, 2:51am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/5 "2006-05-09T02:51:17Z")

</div>

Hi Jeff,  
  
Try this:  
  
1. Define a variable, called “olderthan10days” in Tools - Edit Variables  
2. Use a “Get Date/Time” action, and set it to Use current date, use a format like dd/mm/yyyy or mm/dd/yyyy (probably best to match to your regional setting), and put into the olderthan10days varible.  
3. In the After Action script event of the Get DateTime action, put this VBScript code:  
 olderthan10days = DateAdd(“d”, -10, olderthan10days)  
(DateAdd is a VBScript function, see [http://www.w3schools.com/vbscript/func\_dateadd.asp](http://www.w3schools.com/vbscript/func_dateadd.asp))  
4. Then use this variable in your date/time filter in the FileSet, ie. %olderthan10days% in the “or this date/time (supports FB variables)” field.  
  
cheers,  
.t8

---

<div class="post-metadata">

### Author: ![steve.ng](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@steve.ng](https://www.finalbuilder.com/forums/u/steve.ng)
#### Post date: [May 11, 2006, 5:53am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/6 "2006-05-11T05:53:40Z")

</div>

Hi Tate,  
  
That did the trick perfectly… thanks!

---

<div class="post-metadata">

### Author: ![deleted\_user](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/deleted_user/32/937_2.png) [@deleted\_user](https://www.finalbuilder.com/forums/u/deleted_user)
#### Post date: [November 1, 2007, 8:49am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/7 "2007-11-01T08:49:12Z")

</div>

Hello - I bumped into this problem too   
  
 but I don’t know how to add an After Action script event ?   
  
 In the Final Builder Action I can’t change the action that I didn’t create…   
  
 Thank you.

---

<div class="post-metadata">

### Author: ![Vincent](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/vincent/32/938_2.png) [@Vincent](https://www.finalbuilder.com/forums/u/Vincent)
#### Post date: [November 1, 2007, 11:25am UTC](https://www.finalbuilder.com/forums/t/delete-a-range-of-files/82/8 "2007-11-01T11:25:45Z")

</div>

Select the action in the action list, then on the bottom row of tabs in the IDE, select the Script tab then select the AfterAction tab. You can read about scripting in the help file.
