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
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
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.
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
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)
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
Hi Tate,
That did the trick perfectly… thanks!
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.
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.