Jesthe11

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

7 posts in this topic

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

Share this post


Link to post
Share on other sites

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

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

To add to m23's post, most if not all macros involving directories never have a "/" and thus need to always be added. I have no idea why tho.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Awesome it is working now!

Thank you 

Share this post


Link to post
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