Jump to content

Using Filegettime array in a for loop, pulling from an array


Recommended Posts

So here is the code, I am trying to make it so I can use the array of filegettime to check each folders (directories) creation date in a directory and then delete if they are outside a specified date and time

When I try to use date[1] in my msgbox it tells me it the subscript is inaccessible

 

$localFolders = _FileListToArray(@ScriptDir & "\Local", "*", 2)
                For $localFolder In $localFolders
                    $size = DirGetSize(@ScriptDir & "Local\" & $localFolder)
                    $date = FileGetTime(@ScriptDir & "Local\" & $localFolder,1,0)
                    MsgBox(0,'',$date[1] & @CRLF & $localFolder & @CRLF & Round($size / 1000000,3) & " MB")
                Next

Link to comment
Share on other sites

  • Moderators

Jesthe11,

I t looks as if you are missing a "\" in the path you pass to DirGetSize and FileGetTime:

@ScriptDir & "Local\" & $localFolder

should read:

@ScriptDir & "\Local\" & $localFolder)

You should always check the @error return value from functions which take a path - in this case I suspect they would both have shown the problem.

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

20 hours ago, BetaLeaf said:

I have no idea why tho.

Because (for historical reasons) directory names are just file names with the <directory attribute> set. And filenames cannot contain slash/backslash characters because they are used to indicate directory tree nesting.

Edited by RTFC
Link to comment
Share on other sites

Even with adding the black slash I am getting the same, ERROR: Subscript used on non accessible variable, but for some reason, even without the black slash before the input in DirGetSize it was working..... and I did notice that my filegetime was using the string as an output and not the array, yet that still did not fix the issue when I changed it 

 

$localFolders = _FileListToArray(@ScriptDir & "\Local", "*", 2)
                For $localFolder In $localFolders
                    $size = DirGetSize(@ScriptDir & "\Local\" & $localFolder)
                    $date = FileGetTime(@ScriptDir & "\Local\" & $localFolder,1,0)
                    MsgBox(0,'',$date[1] & @CRLF & $localFolder & @CRLF & Round($size / 1000000,3) & " MB")
                Next

 

Thanks

Link to comment
Share on other sites

  • Moderators

Jesthe11,

I have it - you need to change the syntax for looping through the array. _FileListToArray returns the count in the [0] element - which is taken to be a valid return when using the For...In...Next looping syntax, which in my opinion should only ever be used for object collections. Thus the path is not valid and the function calls fail - I did say you should check the return values.....

When you use an array you should always use the For...To...Next syntax like this:

For $i = 1 To $localFolders[0]
    $size = DirGetSize(@ScriptDir & "\Local\" & $localFolders[$i])
    $date = FileGetTime(@ScriptDir & "\Local\" & $localFolders[$i], 1, 0)
    MsgBox(0, '', $date[1] & @CRLF & $localFolder & @CRLF & Round($size / 1000000, 3) & " MB")
Next

Now you only try to use the valid paths within the array.

By the way, you do realise that you can ask _FileListToArray to return the full path? Just set the $bReturnPath parameter and then you will not need to add the paths within the loop.

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

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...