Jump to content

Help With Array


Ixel
 Share

Recommended Posts

What I am trying to do is search for files and store each file name in a variable. It seemed like an array was an appropriate thing to use here and this script almost works the way I want it to; however, the last message box shows an empty value returned when I expect it to show a file name. What am I doing wrong?

Ixel

AutoItSetOption("WinTitleMatchMode", 1)
$drive=envget("SystemDrive")

$num = FileFindTally($drive & "\Programs\CiscoVPN\Profiles\*.pcf")

Func FileFindTally($fileName)
 $count = 0
 $file = FileFindFirstFile($fileName)
 While Not @error
   If $file <> "." And $file <> ".." Then $count = $count + 1
   $file = FileFindNextFile($file)
 Wend
 Return $count
EndFunc

MsgBox(4096, "File:", $num)

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile($drive & "\Programs\CiscoVPN\Profiles\*.pcf")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No profiles found.  You must use a profile to establish a connection.")
    Exit
EndIf

$num = $num - 1
Global $array[$num]

While 1
    $s = 0 
    $array[$s] = FileFindNextFile($search) 
    If @error Then ExitLoop
    MsgBox(4096, "File:", $array[$s])
    $s = $s + 1
WEnd

; Close the search handle
MsgBox(4096, "File:", $array[0])
FileClose($search)
Link to comment
Share on other sites

too much code for me, heh heh.

simplified version:

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.au3")  
$files=""
; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
$files=$files & ":" & $file
WEnd

; Close the search handle
FileClose($search)
$files=StringSplit($files,":"); create that array of files you wanted, note $files[1] is blank.
For $i= 2 To $files[0]; first one is blank actually by design.
    MsgBox(1,$files[0]-1&"files",$files[$i])
Next

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Ok, cleaner version uses two more if to make sure no blank files and full directory search.

$search = FileFindFirstFile("*.au3")  
Dim $files
If $search = -1 Then; Check if the search was successful
   MsgBox(0, "Error", "No files/directories matched the search pattern")
   Exit
EndIf

While 1
   $file = FileFindNextFile($search)
   If @error Then ExitLoop
if StringLeft($file,1)="." then continueloop; this will remove the . and .. files on a full directory search
If $files="" Then 
    $files=$file
Else
    $files=$files & ":" & $file
EndIf
WEnd

FileClose($search)
$files=StringSplit($files,":")
For $i= 1 To $files[0]
     MsgBox(1,$i &" of "&$files[0]&" files",$files[$i])
Next

Maybe something like this?

or change the return to give you back the full array if you like.

AutoItSetOption("WinTitleMatchMode", 1)
$drive=envget("SystemDrive")
$num = FileFindTally($drive & "\Programs\CiscoVPN\Profiles\*.pcf")

Func FileFindTally($fileName)
$search = FileFindFirstFile($fileName)  
Dim $files
If $search = -1 Then; Check if the search was successful
   MsgBox(0, "Error", "No files/directories matched the search pattern")
   Exit
EndIf

While 1
   $file = FileFindNextFile($search)
   If @error Then ExitLoop
if StringLeft($file,1)="." then continueloop; this will remove the . and .. files on a full directory search
If $files="" Then 
    $files=$file
Else
    $files=$files & ":" & $file
EndIf
WEnd

FileClose($search)
$files=StringSplit($files,":")
return $files[0]
endfunc
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

@scriptkitty

Thanks for your help. At first, I did not understand how your scripts worked until I realized you were using the stringsplit function (which I didn't know existed until now). :whistle: Okay, this is great. Now, how would I insert the array of files into this combo box. The lines below work if a filename is returned for each variable. Do I need to insert a loop here?

Ixel

$profile = Au3GUIControl("combo", "+", 50, 30, -1, -1, "", "", 0)
   Au3GUIData($files[2] & "|" & $files[3] & "|" & $files[4] & "|" & $files[5] & "|" & $files[6], "")
   Au3GUITooltip("Connection profile")
Edited by Ixel
Link to comment
Share on other sites

I would use AutoIt to do most of the typing for ya, yes.

Ok, assuming that this works for the array $files, you can do the same function:

Au3GUIData($files[2] & "|" & $files[3] & "|" & $files[4] & "|" & $files[5] & "|" & $files[6], "")

like this:

$data=$files[2]
for $i= 3 to 6; change to your array notice I skipped the first one
$data=$data & "|" & $files[$i]
next
  Au3GUIData($data,"")

I would have the array fill in the data dynamically:

$data=""
for $i= 1 to $files[0] 
if $data="" then 
$data=$files[$i]
else
$data=$data & "|" & $files[$i]
endif
next
  Au3GUIData($data,"")

if you are using stringsplit, if not then you can use ubound to get the number of elements.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

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