Jump to content

How to: filenames into an array


webjocky
 Share

Recommended Posts

Array n00b here -

I am trying to list all of the files of a certain file-type from a certain directory into an array - without knowing how large the array needs to be.

For example:

Let's say I have a directory "C:\temp\" and there are three files in this directory (but we can't count on only three files being there all the time, could be over 100 files there, we just don't know)

test.exe

somename.log

anothername.log

I need the array to contain the filenames of only the ".log" files.

I have a good idea that it will probably use the following functions:

#include <Array.au3>

_ArrayCreate()

FileFindFirstFile()

FileFindNextFile()

along with a loop and some error traps - but I have been at this for hours on end, and can't seem to get things 'just right'.

Thanks in advance for ANY help

Edit: Just wanted to add that I have searched the forums for anything covering something similar and have come up empty handed

Edited by webjocky

The tiniest of things can ruin your day, or brighten your afternoon. Read it once, read it again, and pay attention. Then search the forums. If all else fails, ask somebody else.

Link to comment
Share on other sites

$pattern = "*.log"
Dim $array[1] 
$handle = FileFindFirstFile($pattern)
If $handle = -1 Then
   MsgBox(4096, "Error", "No files found")
   Exit
EndIf

$counter=0
While 1
   $file = FileFindNextFile($handle)
   If @error Then ExitLoop
   $counter = $counter +1
   ReDim $array[$counter]
   $array[$counter-1]=$file
Wend
FileClose($handle)

$result=""
for $i= 0 to UBound($array)-1
    $result = $result & $array[$i] & " "
Next    
MsgBox(0, $result,"Amound of Files: " & UBound($array))

Edited by Marc

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

heh, thanks guys. Kudos to Marc for doing a lot of legwork, Uten just pulled a shortcut out of his back-pocket - Show-off :)

But they'll both do the trick and thanks for that! :P

The tiniest of things can ruin your day, or brighten your afternoon. Read it once, read it again, and pay attention. Then search the forums. If all else fails, ask somebody else.

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