Jump to content

AutoIT Script folder path problem


Recommended Posts

I am basically listing all the files in a directory in a listbox.

The problem I am having is that the files I want listed in the listbox are in a folder that is within the @ScriptDir. Does anyoner know how I can specify the path to this folder. I have tried _PathFull(@ScriptDir & "\decks"). This did not work. No errors are reported but nothing is listed in the listbox. The folder that contains the files I need to be displayed in the list box is called "decks". It is in the directory where the script resides. Any help would be greatly appreciated.

The following is part of the autoIT SCRIPT:

Func Index()
    $Folder = @ScriptDir
    $Search = FileFindFirstFile($Folder & '\*.*')
    
    While 1
        $File = FileFindNextFile($Search)
        If @error Then ExitLoop
        $Attr = FileGetAttrib($File)
        If Not @error And StringInStr($Attr, 'D') = 0 Then
            $Data = $Data & '|' & $File
        EndIf
    WEnd
    
    FileClose($Search)

    $Data = StringLeft($Data, StringLen($Data) - 1);remove last separator
    $Data = StringRight($Data, StringLen($Data) - 1);remove first separator
    
    ConsoleWrite($Data & @CRLF); Debug Data (Output)
EndFunc ;==>Index

Func Search()
    $savetext = GUICtrlRead($input)
    For $i = 1 To $Split[0]
        If GUICtrlRead($Case) = $GUI_CHECKED Then
            If StringLeft($Split[$i], StringLen(GUICtrlRead($input))) == $savetext Then
                $buffer &= $Split[$i] & "|"
            EndIf
        Else
            If StringLower(StringLeft($Split[$i], StringLen(GUICtrlRead($input)))) == StringLower($savetext) Then
                $buffer &= $Split[$i] & "|"
            EndIf
        EndIf
    Next
    $buffer = StringTrimRight($buffer, 1)
    GUICtrlSetData($list, "")
    GUICtrlSetData($list, $buffer)
    $buffer = ""
EndFunc ;==>Search
Link to comment
Share on other sites

Here is some code from my script/scraps file. More includes than you need but w/e.

Hope this helps

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <String.au3>

opt("GUIOnEventMode", 1)

$maingui = GUICreate("Test", 500, 500, 1, 1)
$btnmain = GUICtrlCreateButton("Click on me", 250, 10, 100, 25)
$lstmain = GUICtrlCreateList("",10,40,220,700)
GUICtrlSetOnEvent($btnmain,"FunRead")
GUISetState()
While 1
    sleep(100)
WEnd

Func FunRead()
    Global $FileList=_FileListToArray(@ScriptDir, "*.txt")
    Select
    Case @error = 1
        MsgBox (0,"","Error with finding directory!")
        Exit    
    Case @error = 4 
        MsgBox (0,"","No Files Found.")
    Case Else
        $max = UBound($FileList)
        $i = 1
        While $i < $max
            GUICtrlSetData($lstmain, $filelist[$i])
            $i = $i+1
        WEnd
    EndSelect
EndFunc
Edited by boogieoompa
Link to comment
Share on other sites

The script you provided was very helpful and resourceful. But how can I specify a path that is different than the @ScriptDir.

From my understanding, @ScriptDir represents the active scripting directory where your script file is right ? But what if I need to specify the path of a folder that is within the @ScriptDir. I appreciate any help. Thank you.

Link to comment
Share on other sites

This?

$search = FileFindFirstFile(@ScriptDir & "\decks\*")
If $search <> -1 Then ;if first file found
    While 1
        $File = FileFindNextFile($search)
        If @error = 1 Then ExitLoop
        If @extended = 1 Then ;if folder
            $Data &= '|' & $File
        EndIf
    WEnd
    FileClose($search)
EndIf
Edited by omikron48
Link to comment
Share on other sites

It's a filter to include everything inside the specified folder.

The FileFind* functions aren't recursive, which mean that they don't search subdirectories by themselves. The example you posted only searches the files located in @ScriptDir. You have to explicitly tell it to search the subfolder you want to search there.

$Folder = @ScriptDir
$Search = FileFindFirstFile($Folder & '\*.*')

This means to search the folder specified by @ScriptDir.

Edited by omikron48
Link to comment
Share on other sites

Easy enough... just change the @scriptdir to your file path, but you have to reference it like a string so you have to include the path in "". If you want to recurse further into your directory than its

@scriptdir & "subfolder"

I dont think you have to put "\subfolder" but I'm too lazy to alt tab to try it lol.

Cheers

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <String.au3>

opt("GUIOnEventMode", 1)

$maingui = GUICreate("Test", 500, 500, 1, 1)
$btnmain = GUICtrlCreateButton("Click on me", 250, 10, 100, 25)
$lstmain = GUICtrlCreateList("",10,40,220,700)
GUICtrlSetOnEvent($btnmain,"FunRead")
GUISetState()
While 1
    sleep(100)
WEnd

Func FunRead()
    Global $FileList=_FileListToArray("C:\Ben", "*.txt")
    Select
    Case @error = 1
        MsgBox (0,"","Error with finding directory!")
        Exit    
    Case @error = 4 
        MsgBox (0,"","No Files Found.")
    Case Else
        $max = UBound($FileList)
        $i = 1
        While $i < $max
            GUICtrlSetData($lstmain, $filelist[$i])
            $i = $i+1
        WEnd
    EndSelect
EndFunc
Link to comment
Share on other sites

Hey all,

This is what ended up working for me:

Func FunRead()

    Local $nmsg

    Global $FileList = _FileListToArray("decks", "*.pdf")

    Select

        Case @error = 1

            MsgBox(0, "", "Error with finding directory!")

            Exit

        Case @error = 4

            MsgBox(0, "", "No Files Found.")

        Case Else

            $max = UBound($FileList)

            $i = 1

            While $i < $max

                GUICtrlSetData($lstmain, $FileList[$i])

                $i = $i + 1
            WEnd

    EndSelect
    Do
        $nmsg = GUIGetMsg()

    Until $nmsg = $GUI_EVENT_CLOSE

EndFunc   ;==>FunRead

I would like to thank boogieoompa and omikron48 for all there help. It is greatly appreciated.

Link to comment
Share on other sites

Just to help improve your coding.

If you have something like this:

$max = UBound($FileList)
$i = 1
While $i < $max
    GUICtrlSetData($lstmain, $FileList[$i])
    $i = $i + 1
WEnd

and the $i isn't used for much anything else other than counting, not calculated from something else, it's much better to use a For..Next loop in that situation like so:

For $i = 1 To UBound($FileList) - 1
    GUICtrlSetData($lstmain, $FileList[$i])
Next
Edited by omikron48
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...