Jump to content
Sign in to follow this  
Wb-FreeKill

Creating Treeview based on txt file...

Recommended Posts

Wb-FreeKill

Okay, a bit hard to explain, but i have a func witch build a treeview by searching trough a path, and then ad folders.. like in Pathfinder from Windows!

But i now want, when the treeview is searching and adding items to the treeview, it should ad the information to a file, witch i can build a treeview like the one i just did, but by reading the information in the file..

Something like:

[-]C:\

......[-]Program Files

.............[-]AutoIt

.....................[-]Include

......[-]Documents and Settings

.............[-]Administrator

So i could build the tree with names by the txt file.. so how can i log this into this file, i haven't found a useful method yet..

Maybe this func could help you... i really need help on this!

_LoadTree("path",$MainTree)
Func _LoadTree($sRoot,$hParent)
    Local $sMask = "*.*"
    Local $aFile[1], $nCnt = 1, $newParent
    Local $hSearch = FileFindFirstFile($sRoot & $sMask)
    Local $sFile
    If $hSearch >= 0 Then
       $sFile = FileFindNextFile($hSearch)
       While not @error
            ReDim $aFile[$nCnt]
            $aFile[$nCnt-1] = $sFile
            $nCnt = $nCnt + 1
            $sFile = FileFindNextFile($hSearch)
       Wend
       FileClose($hSearch)
    EndIf
    For $i = 0 To UBound($aFile) - 1
        If $aFile[$i] == "." or $aFile[$i] == ".." Then ContinueLoop
        If StringInStr(FileGetAttrib($sRoot & "\" & $aFile[$i]), "D") Then
            $newParent = GUICtrlCreateTreeViewItem($aFile[$i],$hParent)
            _LoadTree($sRoot & $aFile[$i] & "\", $newParent)
            ContinueLoop
        Endif
    Next
EndFunc

Thank you...

Edited by Wb-FreeKill

Share this post


Link to post
Share on other sites
GaryFrost

See what you think of this, to use the file to read in you'll have to stringsplit using "\"

_LoadTree("path",$MainTree,1)

Func _LoadTree($sRoot, $hParent, $create_file = 0)
    Local $s_file = @ScriptDir & "\TreeView.log"
    If $create_file Then
        If FileExists($s_file) Then FileDelete($s_file)
        _LogTreeItem($sRoot, "", $s_file); log the main root path
    EndIf
    Local $sMask = "*.*"
    Local $aFile[1], $nCnt = 1, $newParent
    Local $hSearch = FileFindFirstFile($sRoot & $sMask)
    Local $sFile
    If $hSearch >= 0 Then
        $sFile = FileFindNextFile($hSearch)
        While Not @error
            ReDim $aFile[$nCnt]
            $aFile[$nCnt - 1] = $sFile
            $nCnt = $nCnt + 1
            $sFile = FileFindNextFile($hSearch)
        WEnd
        FileClose($hSearch)
    EndIf
    For $i = 0 To UBound($aFile) - 1
        If $aFile[$i] == "." Or $aFile[$i] == ".." Then ContinueLoop
        If StringInStr(FileGetAttrib($sRoot & "\" & $aFile[$i]), "D") Then
            _LogTreeItem($sRoot, $aFile[$i], $s_file);log items root and item path
            $newParent = GUICtrlCreateTreeViewItem($aFile[$i], $hParent)
            _LoadTree($sRoot & $aFile[$i] & "\", $newParent)
            ContinueLoop
        EndIf
    Next
EndFunc  ;==>_LoadTree

Func _LogTreeItem($s_parent, $s_item, $s_file)
    Local $file
    $file = FileOpen($s_file, 1)
    
; Check if file opened for writing OK
    If $file = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    FileWriteLine($file, $s_parent & $s_item)
    
    FileClose($file)
    Return 1
EndFunc  ;==>_LogTreeItem

Gary

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
Wb-FreeKill

Very nice, thanks.. but i don't know how to rebuilt a new tree by using this text... how to tell it were to create the items..

Share this post


Link to post
Share on other sites
Zedna

If it helps,

here is my very old project in Delphi

which is doing exactly same thing.

You may have a look at idea...

procedure TForm1.FormCreate(Sender: TObject);
var
  Pole : array[0..255] of TTreeNode;
  Memo: TMemo;
  Radek: string;
  I, Level: Integer;
begin
  Memo := TMemo.Create (self);
  Memo.Parent := self;
  Memo.Visible := false;
  Memo.Width := 1000;
  Memo.Lines.LoadFromFile ('rodokmen.txt');
  Self.Caption := Memo.Lines[0];
  Application.Title := Memo.Lines[0];

  for I := 2 to Memo.Lines.Count - 1 do begin
    Radek := Memo.Lines[I];
    Level := 1;
    while Radek[Level] = #9 do Inc(Level);
    Dec(Level);
    Radek := Trim(Radek);
    if Level = 0 then
      Pole[Level] := TreeView1.Items.Add (nil, Radek)
    else
      Pole[Level] := TreeView1.Items.AddChild (Pole[Level-1], Radek);
  end; {for}
  Memo.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var CurItem: TTreeNode;
    Memo: TMemo;
    S : string;
begin
  Memo := TMemo.Create (self);
  Memo.Parent := self;
  Memo.Visible := false;
  Memo.Width := 1000;
  Memo.Lines.Add(Self.Caption);
  Memo.Lines.Add('');

  CurItem := TreeView1.Items.GetFirstNode;
  while CurItem <> nil do
  begin
    SetLength(S,CurItem.Level);
    FillChar(S[1],CurItem.Level,#9);
    Memo.Lines.Add(S + CurItem.Text);
    CurItem := CurItem.GetNext;
  end;
  try
    Memo.Lines.SaveToFile ('rodokmen.txt');
  except
    on Exception do; {ignoruje jakoukoli chybu a nedela nic}
  end; {try}
  Memo.Free;
end;

SRC.zip

Share this post


Link to post
Share on other sites
GaryFrost

If FileExists(@ScriptDir & "\TreeView.log") Then
    _LoadTreeFromFile($MainTree, @ScriptDir & "\TreeView.log")
Else
    _LoadTree(GUICtrlRead($DirInput), $MainTree, 1)
EndIf

Func _LoadTree($sRoot, $hParent, $create_file = 0, $Level = 0)
    Local $s_file = @ScriptDir & "\TreeView.log"
    If $create_file Then
        If FileExists($s_file) Then FileDelete($s_file)
        _LogTreeItem($sRoot, $s_file, $Level); log the main root path
    EndIf
    Local $sMask = "*.*"
    Local $aFile[1], $nCnt = 1, $newParent
    Local $hSearch = FileFindFirstFile($sRoot & $sMask)
    Local $sFile
    If $hSearch >= 0 Then
        $sFile = FileFindNextFile($hSearch)
        While Not @error
            ReDim $aFile[$nCnt]
            $aFile[$nCnt - 1] = $sFile
            $nCnt = $nCnt + 1
            $sFile = FileFindNextFile($hSearch)
        WEnd
        FileClose($hSearch)
    EndIf
    For $i = 0 To UBound($aFile) - 1
        If $aFile[$i] == "." Or $aFile[$i] == ".." Then ContinueLoop
        If StringInStr(FileGetAttrib($sRoot & "\" & $aFile[$i]), "D") Then
            _LogTreeItem($aFile[$i], $s_file, $Level + 1);log item path
            $newParent = GUICtrlCreateTreeViewItem($aFile[$i], $hParent)
            _LoadTree($sRoot & $aFile[$i] & "\", $newParent, 0, $Level + 1)
            ContinueLoop
        EndIf
    Next
EndFunc  ;==>_LoadTree

Func _LogTreeItem($s_item, $s_file, $Level)
    Local $file
    $file = FileOpen($s_file, 1)
    
; Check if file opened for writing OK
    If $file = -1 Then
        SetError(1)
        Return 0
    EndIf
    FileWriteLine($file, $Level & "|" & $s_item)
    FileClose($file)
    Return 1
EndFunc  ;==>_LogTreeItem

Func _LoadTreeFromFile($h_treeview, $s_file)
    Dim $file, $a_array, $root, $t_item, $i
    Dim $handles[1]
    $file = FileOpen($s_file, 0)
    
; Check if file opened for reading OK
    If $file = -1 Then
        Return 0
    EndIf
    $a_array = FileRead($file, FileGetSize($s_file))
    FileClose($file)
    $a_array = StringSplit($a_array, @CRLF, 1)
    ReDim $a_array[UBound($a_array) - 1]
    $a_array[0] = $a_array[0] - 1
    $root = StringTrimLeft($a_array[1], 2)
    $handles[0] = $h_treeview
    For $i = 2 To $a_array[0]
        $t_item = StringSplit($a_array[$i], "|")
        
        If Int($t_item[1]) == 1 Then
            ReDim $handles[2]
            $handles[1] = GUICtrlCreateTreeViewItem($t_item[2], $handles[0])
        Else
            If Int($t_item[1]) > UBound($handles) - 1 Then
                ReDim $handles[$t_item[1] + 1]
            EndIf
            $handles[$t_item[1]] = GUICtrlCreateTreeViewItem($t_item[2], $handles[$t_item[1] - 1])
        EndIf
    Next
    Return 1
EndFunc  ;==>_LoadTreeFromFile

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
Wb-FreeKill

Thanks for your time!

But it gives me some errors with this line:

$handles[$t_item[1]] = GUICtrlCreateTreeViewItem($t_item[2], $handles[$t_item[1] - 1])

"$t_item[2]"

Array variable has incorrect numbr of subscript or subscrript dimension range exceeded

"$handles[$t_item[1] - 1])"

Badly formated variable

Share this post


Link to post
Share on other sites
GaryFrost

Make sure your using the latest beta, I'm using 3.1.1.54

Also, make sure to build the new treeview.log, has different format than the 1st 1 I supplied.

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites
Wb-FreeKill

Make sure your using the latest beta, I'm using 3.1.1.54

Also, make sure to build the new treeview.log, has different format than the 1st 1 I supplied.

Gary

<{POST_SNAPBACK}>

hmm somehow it's working now, thanks :)

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.