Jump to content

reading file names + attributes into multi array


Recommended Posts

Example directory contents:

script.au3

folder1

folder2

text.txt

What I would like to be able to do is read an unknown amount of file names from a direcotry with their attribute in the next column (normal file "N" or directory "D")

eg.
       0                  1 
0     script.au3        N

1     folder1          D

2     folder2          D

3     text.txt        N

multi arrays make my brain hurt.

I know how to test the attributes to see weather the file object is a directory etc. Im just not sure how to create and resize the multi array as needed.

Also, how would I go about pritning them out?

Thanks.

my code so far:

#include <Array.au3>

Dim $fileAndType[1][1]

LoadArray($fileAndType, @ScriptDir)

Func LoadArray(ByRef $array, $path)
    FileChangeDir($path)
    
    $search = FileFindFirstFile("*.*")  
    
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        
        If $file <> "." And $file <> ".." Then
        ;???????????????????????????
        EndIf
    WEnd
        
    FileClose($search)
EndFunc
Link to comment
Share on other sites

Do you mean something like this:

#include <Array.au3>

FileChangeDir(@ScriptDir)

$search = FileFindFirstFile("*.*")
If $search <> -1 Then
    $amount = 1
    While 1
        FileFindNextFile($search)
        If @error Then ExitLoop
        $amount = $amount + 1
    WEnd
    Dim $fileandattributes[$amount + 1][2]
    $fileandattributes[0][0] = $amount
    $search = FileFindFirstFile("*.*")
    $fileandattributes[1][0] = $search
    $fileandattributes[1][1] = FileGetAttrib(@ScriptDir & "\" & $search)
    For $i = 1 To $amount - 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $fileandattributes[$i][0] = $file
        $fileandattributes[$i][1] = FileGetAttrib(@ScriptDir & "\" & $file)
    Next
    FileClose($search)
EndIf

Hope you do :D

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

  • Moderators

This was interesting, I could have swore I did this before... but I couldn't find it :D ... but I played with it all the same: 3 types of arrays here... [Number][0] will give the file name only ... [0][Number] will give the Attribute only... [Number][Number] will give the File Name and Attribute seperated by '|' (which can obviously be changed):

$aArray = _CreateFAArray(@HomeDrive & '\*')
If IsArray($aArray) Then
    For $i = 1 To UBound($aArray) - 1
        ConsoleWrite('File found is: ' & $aArray[$i][0] & ' File Attribute is: ' & $aArray[0][$i] & '   :   ' & $aArray[$i][$i] & @LF)
    Next
EndIf

Func _CreateFAArray($h_FileDirectory_Name = '*', $s_Mask = '.*')
    $nSearch = FileFindFirstFile($h_FileDirectory_Name & $s_Mask)
    If $nSearch <> -1 Then
        Local $iCount = 0, $hFileName = '', $sFileAttrib = ''
        While 1
            $nNextSearch = FileFindNextFile($nSearch)
            If @error Then ExitLoop
            $iCount += 1
            $hFileName &= $nNextSearch & Chr(01)
            If $h_FileDirectory_Name = '*' Then 
                $sFileAttrib &= FileGetAttrib($nNextSearch) & Chr(01)
            Else
                $sFileAttrib &= FileGetAttrib(StringTrimRight($h_FileDirectory_Name, 1) & $nNextSearch) & Chr(01)
            EndIf
        WEnd
        $hFileName = StringSplit(StringTrimRight($hFileName, 1), Chr(01))
        $sFileAttrib = StringSplit(StringTrimRight($sFileAttrib, 1), Chr(01))
        Local $Create2Dim[UBound($hFileName)][UBound($sFileAttrib)]
        $Create2Dim[0][0] = UBound($hFileName)
        For $xCount = 1 To UBound($hFileName) - 1
            $Create2Dim[$xCount][0] = $hFileName[$xCount]
            $Create2Dim[0][$xCount] = $sFileAttrib[$xCount]
            $Create2Dim[$xCount][$xCount] = $hFileName[$xCount] & '|' & $sFileAttrib[$xCount]
        Next
        Return $Create2Dim
    EndIf
    FileClose($nSearch)
    SetError(1)
    Return 0
EndFunc
I didn't play much with how to find the files, please note the '\*' in the parameter I'm sending to the function '*' being a wild card to find all the files/folders.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Well let's call that settled then :D

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

Sorry to be a pest, but two questions:

What is this line testing for?

If $h_FileDirectory_Name = '*' Then     
    $sFileAttrib &= FileGetAttrib($nNextSearch) & Chr(01)

And... I am samilar with C arrays, but not autoit, I am trying to get a clear image of the array matrix in my head.

eg. C language array

int intArray[3][4];

        0       1       2       3  
0     00        01     02      03

1     10        10     12      13
 
2     20        21     22      23

How would the autoit array of $aArray[4][4] look?

I just dont follow how in the example you gave it was decleared as $aArray[4][4] but each element has 3 components. eg [1][0] = filename [0][1] = attributes [1][1] = filename and attributes

Thanks alot

Edited by creeping
Link to comment
Share on other sites

  • Moderators

Sorry to be a pest, but two questions:

What is this line testing for?

If $h_FileDirectory_Name = '*' Then     
    $sFileAttrib &= FileGetAttrib($nNextSearch) & Chr(01)

And... I am samilar with C arrays, but not autoit, I am trying to get a clear image of the array matrix in my head.

eg. C language array

int intArray[3][4];

        0       1       2       3  
0     00        01     02      03

1     10        10     12      13
 
2     20        21     22      23

How would the autoit array of $aArray[4][4] look?

I just dont follow how in the example you gave it was decleared as $aArray[4][4] but each element has 3 components. eg [1][0] = filename [0][1] = attributes [1][1] = filename and attributes

Thanks alot

I'm setting up the string there to make it an Array later with StringInString(), if you look there it creates the array using the Chr(01) as a delimeter. += / &= / etc... are all Beta functions if your running into an issue there, then download the beta here Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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