Jump to content

arrays


Recommended Posts

I am trying to use the Function 'FileOpenDialog' to select multiple files using the [Ctrl key] and then trying to convert the selected files into an array.

I cannot find an example in Help that shows how this is achieved.

This code does not work:

#include <file.au3>

;Choose Image to Process

$iFiles = FileOpenDialog("Select the Image File/s", @MyDocumentsDir, "Image Files (*.jpg;*.bmp)", 1 + 4)

$Filelist = _FileReadToArray("Test",$iFiles)

_ArrayDisplay($FileList,"Test")

Can anyone help me with how this type of user input can be achieved

Ant..

Link to comment
Share on other sites

Here..

#include<Array.au3>
Global $Dir[256]

;Choose Image to Process
$iFiles = FileOpenDialog("Select the Image File/s", @WindowsDir, "Image Files (*.jpg;*.bmp)", 1 + 4)
$Split=StringSplit($iFiles,"|")
For $i=2 to UBound($Split,1)-1
    $Dir[$i]=$Split[$i]
Next
_ArrayDisplay($Dir)

;Example to trim off, just some addition if you need
$Dirc = FileOpenDialog("Please Select the File", @DesktopDir, "All (*.*)", 1 + 2)
$String = StringSplit($Dirc, ".")
$String2 = StringSplit($String[1], "\")
$Display = UBound($String2, 1) - 1

MsgBox(64,"Trimmed off file extension",$String2[$Display])
Link to comment
Share on other sites

Here..

#include<Array.au3>
Global $Dir[256]

;Choose Image to Process
$iFiles = FileOpenDialog("Select the Image File/s", @WindowsDir, "Image Files (*.jpg;*.bmp)", 1 + 4)
$Split=StringSplit($iFiles,"|")
For $i=2 to UBound($Split,1)-1
    $Dir[$i]=$Split[$i]
Next
_ArrayDisplay($Dir)

;Example to trim off, just some addition if you need
$Dirc = FileOpenDialog("Please Select the File", @DesktopDir, "All (*.*)", 1 + 2)
$String = StringSplit($Dirc, ".")
$String2 = StringSplit($String[1], "\")
$Display = UBound($String2, 1) - 1

MsgBox(64,"Trimmed off file extension",$String2[$Display])
Thanks for the help in the array your code gives me the first selected file in [2] nothing in [1] and nothing in [0] what I need is [0] to equal the number of files selected as per example and then [1] the first file [2] the second and so on"

[0] 2

[1] xyz.jpg

[2] abc.jpg

Help is always appreciated

Ant..

Link to comment
Share on other sites

Not sure, since it's not really a big deal, well i don't think it's possible in this way(For Next loop) but try this,

Change

$Dir[$i]=$Split[$i]

To

$Dir[$i-2]=$Split[$i]

Hi that mod produces the following:

C:\PLImage\Source Code\ProcessImages.au3 (244) : ==> Array variable subscript badly formatted.:

$Dir[$i-2]=$Split[$i]

$Dir[^ ERROR

Ant

Edit

Back in 24 hours

Edited by anixon
Link to comment
Share on other sites

I am trying to use the Function 'FileOpenDialog' to select multiple files using the [Ctrl key] and then trying to convert the selected files into an array.

I cannot find an example in Help that shows how this is achieved.

...

Here is an example that may help.

#include<Array.au3>
Global $file[2]

;Choose file(s) to Process
$iFiles = FileOpenDialog("Select TXT or AU3 File/s", @MyDocumentsDir, "Image Files (*.txt;*au3)", 1 + 4)
$Split = StringSplit($iFiles,"|")

Global $x = 1
For $i = 1 to UBound($Split) -1
    $file_contents = FileRead($Split[$i])
    If $file_contents <> '' Then
        ; Add an element to the array
        $file[$x] = $file_contents
        $x += 1
    Else
        ContinueLoop
    EndIf
    ; Increase the array size by 1
    ReDim $file[UBound($file) +1]
Next
; Cleanup a variable no longer needed
$file_contents = ''
; Last element expected to be empty so correct the count
If $file[UBound($file) - 1] = '' Then
    ReDim $file[UBound($file) -1]
EndIf
; Add count to 1st element of the array
$file[0] = UBound($file) -1
; Display the results
_ArrayDisplay($file)

:)

Edited by MHz
Link to comment
Share on other sites

Here is an example that may help.

#include<Array.au3>
Global $file[2]

;Choose file(s) to Process
$iFiles = FileOpenDialog("Select TXT or AU3 File/s", @MyDocumentsDir, "Image Files (*.txt;*au3)", 1 + 4)
$Split = StringSplit($iFiles,"|")

Global $x = 1
For $i = 1 to UBound($Split) -1
    $file_contents = FileRead($Split[$i])
    If $file_contents <> '' Then
        ; Add an element to the array
        $file[$x] = $file_contents
        $x += 1
    Else
        ContinueLoop
    EndIf
    ; Increase the array size by 1
    ReDim $file[UBound($file) +1]
Next
; Cleanup a variable no longer needed
$file_contents = ''
; Last element expected to be empty so correct the count
If $file[UBound($file) - 1] = '' Then
    ReDim $file[UBound($file) -1]
EndIf
; Add count to 1st element of the array
$file[0] = UBound($file) -1
; Display the results
_ArrayDisplay($file)

:)

Thanks very much for the code had to make two changes for it to work as required

For $i = 2 to UBound($Split)-1

$i = 1 changed to $i = 2 to eliminate the path record

and

$file_contents = FileRead($Split[$i])

Changed to

$file_contents = $Split[$i]

to change the filename from garbage to its proper name

Thanks for the help

Ant..

Link to comment
Share on other sites

In order to make the things easier for you: keep in mind that when you select multiple files the output of FileOpenDialog looks like:

i.e. you have 3 files into C:\temp folder a.jpg, b.jpg, c.jpg - once you select them all you will get a string like: C:\temp|a.jpg|b.jpg|c.jpg

So in order to get the filenames into an array (depends if you want the full path or only the file name) you have to split this string using "|" as a delimiter.

Here it is an example which stores the file names into array elements and the number on the element 0 - if you want to include the path aswell as the filename - the script will require q small modification:

$tt = FileOpenDialog("Files", "C:\", "Text files (*.txt)", 4)
MsgBox(0, "tt", $tt)
$array = StringSplit($tt, "|")
;$array[0] will have the number of files + the folder
;$array[1] will be the path
;$array[2] $array[3].... will have the file names

;this example will create an array where on element 0 you will have the number of elements
Dim $file_array [UBound($array) - 1]    ;declare the new array with a length of the prevuious array -1 (minus the path)
$counter = 0            ;used to count the elements
$dsp = ""               ;for testing - to show the elements
For $i=2 To UBound($array) - 1      ;$i=2 because we skip first element (path)
    $file_array[$i -1] = $array[$i]
    $counter = $counter + 1
Next
$file_array[0] = $counter
For $i=1 to UBound($file_array) - 1     ;just for testing - creating the display string out of your array elements
    $dsp &= $file_array[$i]&"  "
Next
MsgBox(0, "Files in the result array", $dsp)
MsgBox(0, "nbr", $file_array[0])

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

In order to make the things easier for you: keep in mind that when you select multiple files the output of FileOpenDialog looks like:

i.e. you have 3 files into C:\temp folder a.jpg, b.jpg, c.jpg - once you select them all you will get a string like: C:\temp|a.jpg|b.jpg|c.jpg

So in order to get the filenames into an array (depends if you want the full path or only the file name) you have to split this string using "|" as a delimiter.

Here it is an example which stores the file names into array elements and the number on the element 0 - if you want to include the path aswell as the filename - the script will require q small modification:

$tt = FileOpenDialog("Files", "C:\", "Text files (*.txt)", 4)
MsgBox(0, "tt", $tt)
$array = StringSplit($tt, "|")
;$array[0] will have the number of files + the folder
;$array[1] will be the path
;$array[2] $array[3].... will have the file names

;this example will create an array where on element 0 you will have the number of elements
Dim $file_array [UBound($array) - 1]    ;declare the new array with a length of the prevuious array -1 (minus the path)
$counter = 0            ;used to count the elements
$dsp = ""               ;for testing - to show the elements
For $i=2 To UBound($array) - 1      ;$i=2 because we skip first element (path)
    $file_array[$i -1] = $array[$i]
    $counter = $counter + 1
Next
$file_array[0] = $counter
For $i=1 to UBound($file_array) - 1     ;just for testing - creating the display string out of your array elements
    $dsp &= $file_array[$i]&"  "
Next
MsgBox(0, "Files in the result array", $dsp)
MsgBox(0, "nbr", $file_array[0])

Thanks for that very helpful and good to catch up again

Ant..

Link to comment
Share on other sites

Thanks very much for the code had to make two changes for it to work as required

For $i = 2 to UBound($Split)-1

$i = 1 changed to $i = 2 to eliminate the path record

and

$file_contents = FileRead($Split[$i])

Changed to

$file_contents = $Split[$i]

to change the filename from garbage to its proper name

Thanks for the help

Ant..

Hmmm, define garbage? If the garbage you call it is the contents of the file, then your topic did not specify file names into an array. The the correction made by you on the path record, sorry, yet tested again and I see no path record :). Perhaps you could enlighten me.
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...