Jump to content

Trouble redimming array and accessing array in another function


jack71
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

I'm trying to search a DIR for .XML files, filter those files so the array only contains files that are 48 hours and younger, then create a 2D Array so I can sort the XML files by date from newest to oldest.

Here's what I have:

#include <Array.au3>
#include <File.au3>
#include <date.au3>

Global $aFiles

RemOldXML()

Func RemOldXML()
local $path, $aFiles, $aFilesTime, $TimeArray, $redim = 0

$sPath = @ScriptDir
$aFiles = _FileListToArrayRec(@ScriptDir, "*.xml", 1, 0, 0, 2)
_ArrayDisplay($aFiles, "original xml file array")

For $i = UBound($aFiles) -1 To 1 Step -1
$aFilesTime = FileGetTime($aFiles[$i], 1, 0)
$TimeArray = StringFormat("%s/%s/%s %s:%s:%s", $aFilestime[0], $aFilestime[1], $aFilestime[2], $aFilestime[3], $aFilestime[4], $aFilestime[5])

    If _DateDiff('h', $TimeArray, _NowCalc()) >= 49  Then
    _ArrayDelete($aFiles, $i)
    $redim = $redim + 1
    _ArrayDisplay($aFiles, "Filtered Array")
    EndIf
Next

 ReDim $aFiles[UBound($aFiles) - $redim]
_ArrayDisplay($aFiles, "Final array")

SortXML()
EndFunc



Func SortXML()
local $aFileList2D

$aFileList2D[$aFiles[0] + 1][2] = [$aFiles[0]]

For $i = 1 To $aFiles[0]

$aFileList2D[$i][0] = $aFiles[$i]

$aFileList2D[$i][1] = FileGetTime($aFiles[$i], 1, 1)

Next

_ArraySort($aFileList2D, 1, 0, 0, 1)

_ArrayDisplay($aFileList2D, "Final Sorted Array")
EndFunc

Trouble I'm having is when I try to redim the final filtered array ($aFiles), on my computer I have 2 .xml files that are > 48 hours old.  So my final filtered $aFiles has 2 entries ([0][1][2]) but _arraydisplay says there's 4 entries.  So it's not resizing correctly.

When I throw in the SortXML() to the mix I can't even run the script.  For whatever reason after _arraydisplay $afiles gives me my resultant fitlered array....the array is no longer an array.  If I put _ArrayDisplay at the top after the first function I get an IsArray error of 0. When called on in SortXML() and I get this error:

error: syntax error $aFileList2D[$aFiles[0] + 1][2] = [

Please educate me on my errors :)

Link to comment
Share on other sites

  • Solution

There's no need to ReDim the array after you've found all the old files, _ArrayDelete is already doing that for you. You can use the $redim variable to keep track of how many items were deleted, that way you can update $afiles[0] with the correct number of items in the array after it's done deleting.

Also, your script wouldn't run as written anyways because the $afiles array is local to the RemOldXML function and the SortXML function wouldn't be able to access it. I've updated the script with some changes that will work as I believe you wanted it to. Look for the comment lines with <<<<<<<< in them

#include <Array.au3>
#include <File.au3>
#include <date.au3>

Global $aFiles

RemOldXML()

Func RemOldXML()
    Local $path, $aFiles, $aFilesTime, $TimeArray, $redim = 0

    $sPath = @ScriptDir
    $aFiles = _FileListToArrayRec(@ScriptDir, "*.xml", 1, 0, 0, 2)
    _ArrayDisplay($aFiles, "original xml file array")
        $redim = UBound($aFiles) - 1 ; <<<<<<<<<<<< created the variable with the original value of the Ubound of $afiles
    For $i = UBound($aFiles) - 1 To 1 Step -1
        $aFilesTime = FileGetTime($aFiles[$i], 1, 0)
        $TimeArray = StringFormat("%s/%s/%s %s:%s:%s", $aFilesTime[0], $aFilesTime[1], $aFilesTime[2], $aFilesTime[3], $aFilesTime[4], $aFilesTime[5])

        If _DateDiff('h', $TimeArray, _NowCalc()) >= 49 Then
            _ArrayDelete($aFiles, $i)
            $redim -= 1 ; <<<<<<<<<<< changed this to a subtraction, lowers the value by one everytime you delete a line from the array
        EndIf
    Next
    $aFiles[0] = $redim ;<<<<<<<<<<<<< Update the value in $afiles[0] to equal the new count 
    _ArrayDisplay($aFiles, "Final array")

    SortXML($aFiles) ; <<<<<<<<<<<< pass the array to the SortXML function
EndFunc   ;==>RemOldXML



Func SortXML($aFiles) ; <<<<<<<<<<<< changed the function to accept a parameter
    Local $aFileList2D[$aFiles[0] + 1][2]= [[$aFiles[0], ""]] ; <<<<<<<<<<<<<<<<<< corrected the array initiliazation


    For $i = 1 To $aFiles[0]

        $aFileList2D[$i][0] = $aFiles[$i]

        $aFileList2D[$i][1] = FileGetTime($aFiles[$i], 1, 1)

    Next

    _ArraySort($aFileList2D, 1, 0, 0, 1)

    _ArrayDisplay($aFileList2D, "Final Sorted Array")
EndFunc   ;==>SortXML
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

We all had to learn it. :)

BTW, did it work the way you wanted it to?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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