Sign in to follow this  
Followers 0
webjocky

How to: filenames into an array

7 posts in this topic

#1 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$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

It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

°oO( Cheater )


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

*pwned* :)


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


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

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
Sign in to follow this  
Followers 0