Jump to content

Recommended Posts

Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

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.

 

 

Posted (edited)
  On 5/29/2016 at 12:02 PM, BetaLeaf said:

I have no idea why tho.

Expand  

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
Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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