Sign in to follow this  
Followers 0
Danger111

Treeview and Folders/Files

13 posts in this topic

Case $MenuItem2
   $Step1 = MsgBox(1, "Type", "Do you want to create New Dir?")
   If $Step1 = 1 Then
    $Add = FileSelectFolder("Choose your folder:", "")
    $CIcon = InputBox("Icon", "Choose the Icon, (You can see Icons in Icon List):")
    $New = _GUICtrlTreeView_Add($hTreeView, 0, String($Add), $CIcon, $CIcon)
    $Path = InputBox("Folder Path", "Type the folder Path:")
    $Search = FileFindFirstFile($Path & "*.*")
    While 2
     $Files = FileFindNextFile($Search)
     If @error Then ExitLoop
     $Attrib = FileGetAttrib($Files)
     If StringInStr($Attrib, "D") Then
      $IconNumber = 3
     EndIf
     ;If StringRight(string($Files), 4) <> "." Then
      ;$IconNumber = 3
     ;EndIf
     _GUICtrlTreeView_AddChild($hTreeView, $New, $Files, $IconNumber, $IconNumber)
    WEnd
   EndIf

Here is my code everything goes okay except:

$Attrib = FileGetAttrib($Files)
If StringInStr($Attrib, "D") Then
      $IconNumber = 3
     EndIf

So it checks if founded file is directory, if it is directory it gives it folder icon. But it does not work.

This method works, but it wont work if file has 4 letter after ".":

If StringRight(string($Files), 4) <> "." Then
      $IconNumber = 3
     EndIf

And one more thing: How can i make, that if in "scaned" folder there are other folders, script to "scan" them too?

Thanks

Share this post


Link to post
Share on other sites



Danger111,

No need for you to code the "is it a folder check", as FileFindNextFile does it for you by setting @extended to 1 if the returned filename is a directory. ;)

As to looking in folders within folder, I suggest you start by reading the Recursion tutorial in the Wiki. Recursion is very powerful but you must be very careful when you use it.

:)

Finally, you might want to look at the ChooseFileFolder UDF in my sig in which I produce a TreeView of a given path. Feel free to copy any of the code that you might think useful. ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Danger111,

No need for you to code the "is it a folder check", as FileFindNextFile does it for you by setting @extended to 1 if the returned filename is a directory. :)

As to looking in folders within folder, I suggest you start by reading the Recursion tutorial in the Wiki. Recursion is very powerful but you must be very careful when you use it.

;)

Finally, you might want to look at the ChooseFileFolder UDF in my sig in which I produce a TreeView of a given path. Feel free to copy any of the code that you might think useful. ;)

M23

Thank you very much :)

Share this post


Link to post
Share on other sites

I have one more question: Is there such function, script to search some word or sentence in txt file?

Share this post


Link to post
Share on other sites

Danger111,

FileRead gets you the content; StringInStr finds a text string within it. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

perhaps?

Edit: This is if you want to search for txt files in a directory with a keyword. Otherwise FileRead & StringInStr.

Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I know i have too many questions but i have one more :) : how to get text of the selected item in treeview? this method does not work:

$Item = GUICtrlRead($TreeView2)
   If $Item = 0 Then
    MsgBox(0, "Error", "Error appeared")
   Else
   $Text = GUICtrlRead($Item, 1)
   if $text == "" Then
    MsgBox(0, "Error", "Error appeared1")
   Else
   MsgBox(64,"Result", "The name of the selected file is:" &$Text)
  EndIf
  EndIf

I added treeview items with _GUICtrlTreeView_Add and _GUICtrlTreeView_Addchild

Edited by Danger111

Share this post


Link to post
Share on other sites

Danger111,

If you used the UDF to create the TreeView and its items then you need to use the UDF to interact with it. Try _GUICtrlTreeView_GetText. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Danger111,

If you used the UDF to create the TreeView and its items then you need to use the UDF to interact with it. Try _GUICtrlTreeView_GetText. :)

M23

Thanks again for the help, i used _GUICtrlTreeView_GetSelection and _GUICtrlTreeView_GetText and it worked very well.

And here is my last question:

For Example i have a txt file, where there is written a text of 20 lines, FileRead and StringInStr can found a word there and is it possible to save the whole line on which the word is ( i mean for example if the word "Barrel" is on the third line script to save the whole line?)

Edit: So to better understand what i meant by saving, i will tell you why i need this. So here is a function i use to scan folders:

Func ListFiles_ToTreeView($sSourceFolder, $hItem)
    Local $sFile
    ; Force a trailing 
    If StringRight($sSourceFolder, 1) <> "" Then $sSourceFolder &= ""
    ; Start the search
    Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*")
    ; If no files found then return
    If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
    ; Now run through the contents of the folder
    While 1
        ; Get next match
        $sFile = FileFindNextFile($hSearch)
        ; If no more files then close search handle and return
        If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
        ; Check if a folder
        If @extended Then
            ; If so then call the function recursively
            ListFiles_ToTreeView($sSourceFolder & $sFile, _GUICtrlTreeView_AddChild($TreeView1, $hItem, $sFile,3,3))
        Else
            ; If a file than write path and name
   If StringRight(string($sFile), 3) = "mp3" Then
    $IconNumber = 168
   ElseIf StringRight(string($sFile), 3) = "txt" Then
    $IconNumber = 70
   ElseIf StringRight(string($sFile), 3) = "jpg" Then
    $IconNumber = 141
   ElseIf StringRight(string($sFile), 3) = "avi" Then
    $IconNumber = 165
   EndIf
   If Not FileExists(@ScriptDir & "File Pathes.txt") Then
   FileWriteLine(@ScriptDir & "File Pathes.txt", $sSourceFolder & "" & $sFile)
   EndIf
            _GUICtrlTreeView_AddChild($TreeView1, $hItem, $sFile,$IconNumber,$IconNumber)
   $IconNumber = 0
        EndIf
    WEnd

and here is what i scan (the whole Computer):

Local $DrivePath[7]
$DrivePath[1] = "C:"
$DrivePath[2] = "D:"
$DrivePath[3] = "E:"
$DrivePath[4] = "F:"
$DrivePath[5] = "G:"
$DrivePath[6] = "H:"
$N = 1
_GUICtrlTreeView_BeginUpdate($TreeView1)
For $i = 1 to $Drives[0]
$DriveCreate = _GUICtrlTreeView_Add($TreeView1, 0, StringUpper($Drives[$i]), 8,8)
ListFiles_ToTreeView($DrivePath[$N], $DriveCreate)
$N = $N + 1
Next
_GUICtrlTreeView_EndUpdate($TreeView1)

This function opens a txt file in script directory and writes the full path of each founded file.

By getting text of the selected item, i want script to search the text (name) of the selected item, and when he founds it to copy the whole line (Full path) of it, i will use this path then to open the selected file with Run function

Edited by Danger111

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

So i have found this function which gets line number with the string

Func _LineNumsOfSearchStr($FileToOpen, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($FileToOpen) = 0 Then Return 1
    Do
  $FFFile = FileRead($FileToOpen)
        $location = StringInStr($FFFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"
        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($FFFile, "(?s)(.{" & $location & "})(.*)$", "1"), "(?s)(v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
                _FileWriteToLine($FileToOpen, $iCurrentLineNum, "", 1) ; Remove found line from file.
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr

So this is exactly what i want but there is a one problem: this function does not work if a string is on more that 57000 line. Can anybody tell me whats wrong?

Edit: I reposted the func code, it was wrong

Edited by Danger111

Share this post


Link to post
Share on other sites

Danger111,

It looks to me as if your TreeView already holds the file path within it - each time you find a folder you start a new branch. So you should be able to get the path by using _GUICtrlTreeView_GetTree - no need for a file at all. ;)

Try running this: ;)

#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>

Global $aHandles[100] = [0]
$sPath = "C:" ; Amend this as you wish

$hGUI = GUICreate("Test", 500, 500)

$TreeView1 = _GUICtrlTreeView_Create($hGUI, 10, 10, 480, 380, BitOr($TVS_CHECKBOXES, $TVS_HASLINES) )
_GUICtrlTreeView_BeginUpdate($TreeView1)
$DriveCreate = _GUICtrlTreeView_Add($TreeView1, 0, $sPath, 8, 8)
ListFiles_ToTreeView("M:MusicMP3", $DriveCreate)
_GUICtrlTreeView_EndUpdate($TreeView1)
_GUICtrlTreeView_Expand($TreeView1)
ReDim $aHandles[$aHandles[0] + 1]

$hButton = GUICtrlCreateButton("Read selected", 10, 450, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            For $i = 0 To $aHandles[0]
                If _GUICtrlTreeView_GetChecked($TreeView1, $aHandles[$i]) = True Then
                    MsgBox(0, "Path", _GUICtrlTreeView_GetTree($TreeView1, $aHandles[$i]))
                EndIf
            Next
    EndSwitch

WEnd

Func ListFiles_ToTreeView($sSourceFolder, $hItem)
    Local $sFile
    ; Force a trailing 
    If StringRight($sSourceFolder, 1) <> "" Then $sSourceFolder &= ""
    ; Start the search
    Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*")
    ; If no files found then return
    If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
    ; Now run through the contents of the folder
    While 1
        ; Get next match
        $sFile = FileFindNextFile($hSearch)
        ; If no more files then close search handle and return
        If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
        ; Check if a folder
        If @extended Then
            ; If so then call the function recursively
            $aHandles[0] += 1
            If UBound($aHandles) <= $aHandles[0] Then ReDim $aHandles[UBound($aHandles) * 2]
            $aHandles[$aHandles[0]] = _GUICtrlTreeView_AddChild($TreeView1, $hItem, $sFile, 3, 3)
            ListFiles_ToTreeView($sSourceFolder & $sFile, $aHandles[$aHandles[0]])
        Else
            ; If a file than write path and name
            If StringRight(String($sFile), 3) = "mp3" Then
                $IconNumber = 168
            ElseIf StringRight(String($sFile), 3) = "txt" Then
                $IconNumber = 70
            ElseIf StringRight(String($sFile), 3) = "jpg" Then
                $IconNumber = 141
            ElseIf StringRight(String($sFile), 3) = "avi" Then
                $IconNumber = 165
            Else
                $IconNumber = 0
            EndIf
            $aHandles[0] += 1
            If UBound($aHandles) <= $aHandles[0] Then ReDim $aHandles[UBound($aHandles) * 2]
            $aHandles[$aHandles[0]] = _GUICtrlTreeView_AddChild($TreeView1, $hItem, $sFile, $IconNumber, $IconNumber)
        EndIf
    WEnd
EndFunc

You can see I use the trick I showed in the Recursion tutorial to size the array. :)

I hope it works for you - it does for me. :)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23

Thank you very much again.

It worked for me aswell, just it showed pathes with "|" instead of "" but i used Stringreplace for that and everything worked fine.

Share this post


Link to post
Share on other sites

Danger111,

Glad I could help. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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