Jump to content

Script to Remove FOLDERS older than X days


Recommended Posts

  • Moderators

bgruett,

The error means that the script could not get the date from the file - hence there is not an array to access. ;)

The obvious question is: How are you defining the $sRoot path variable? Are you adding a trailing backslash? If not, then the path you create with:

$aDate = FileGetTime($sRoot & $aList[$i], 0, 0)

will be missing a "" and so the command fail - which is what I imagine to be happening.

Solutions would be:

- 1. Add a trailing backslash to the $sRoot variable

- 2. Add the backslash when you create the path:

$aDate = FileGetTime($sRoot & "" & $aList[$i], 0, 0)

Personally, I would always check the initial assignment of the $sRoot variable and add a backslash if required:

If StringRight($sRoot, 1) <> "" Then
    $sRoot &= ""
EndIf

Then you need not worry about it from then on. ;)

Any use? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Heh heh...that trailing backslash was missing, yes. And by putting it in, I'm no longer getting the error in question.

Still not through the woods yet. Looks like the properly-formatted dates aren't so properly formatted after being processed by the script.

When the array is cast, I get rows 0-5 in the following order: [YEAR][MONTH][DAY OF MONTH][HOUR IN 24-HOUR FORMAT][MINUTES][sECONDS]. However, the formatted date dispays as follows: [MINUTES]/[MONTH]/[DAY OF MONTH].

Essentially it looks like the [YEAR] field is being replaced in the formatting by the [MINUTES] field instead.

I'm going to try and figure this last part out myself, since you guys have been so helpful. Thanks again!

-Bob

Edited by bgruett
Link to comment
Share on other sites

Okay, so really dumb error on my part, and really simple solution. The issue was that I was loading the wrong field from the array into the formatted date variable. Pretty obvious, and makes perfect sense in retrospect, but since I'm still learning arrays it wasn't clear up front.

Anyway, the current script (modified and ripped from various sources on these boards...and tested working in my environment) follows:

#include <Array.au3>
#include <File.au3>
#include <Date.au3>
; put the root in a variable
$sRoot = "C:Test"
; Retrieve a list of all the folders in $sRoot, and store that list as an array in $aList
Global $aList = _FileListToArray($sRoot, "*", 2) ; _FileListToArray("path" [, "Filter" [, Flag]])
; Look at what _FileListToArray() puts into $aList
; You can delete the below line after seeing the output for the first time
;_ArrayDisplay($aList)
; This is a loop that runs from 1 to the number of items listed in the first element of the returned  array
For $i = 1 To $aList[0]
  ; For each folder, get the modified date/time and store its individual parts in array $aDate
    $aDate = FileGetTime($sRoot & $aList[$i], 0, 0) ; FileGetTime("filename" [, option [, format]])
    ; Look at what FileGetTime() puts into $aDate
    ; You can delete the below line after seeing the output for the first time
    ;_ArrayDisplay($aDate)
    ; Build the properly formatted date string for _DateDiff()'s needs
    $sDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2] ;0 index stores the year, 1 index stores month, 2 index stores day
    ; Look at what we made into $sDate
    ; You can delete the below line after seeing the output for the first time
    ;msgbox(0,"Check Output", $sDate)
    ; Use _DateDiff() to see if the difference is greater than 3 days, and if it's true, then delete the folder and all its subfolders
If _DateDiff("D", $sDate, _NowCalcDate()) > 3 Then DirRemove($sRoot & $aList[$i], 1)
Next

Obviously I take zero credit for this. Which is probably why it works so beautifully! :D

Thanks,

Bob

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...