david1337

Show content of all txt files in a folder sorted by rows

8 posts in this topic

#1 ·  Posted (edited)

Hi guys

I have a folder --> "C:\Test\" containing different txt files named: test1.txt, test2.txt, test3.txt etc.
Each txt file contains lists of names seperated by line breaks like:

Content of test1.txt;
Jack
Marcus
Michael
etc etc...

Is it possible to show the content of all txt files in 1 list/view, separated with rows that are named after the name of the txt file:

test1   | test2   | test 3   |
________|_________|__________|
Jack    | Kim     | Jennifer |
Marcus  | Britney | John     |
Michael | Tom     | Brad     |

 

I haven't come closer in a script than to show the content of a single file in an msgbox, and that is without the row headline, "test1" :-(

 

$File1 = "C:\Test\test1.txt"
MsgBox(0, "Content of File", FileRead($File1))


         

Edited by david1337

Share this post


Link to post
Share on other sites



You are going to want to use _FileListToArray to add each file and then just step through the arrays formatting them for your listview.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You can do something like this:

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
Global $g_hListView

Example()

Func Example()
    Local $hGUI, $hImage
    $hGUI = GUICreate("Litview", 400, 300)
    Local $sDirPath = "C:\test"
    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState(@SW_SHOW)

    Local $aFiles = _FileListToArray($sDirPath)
;~  _ArrayDisplay($aFiles)


    Local $aString = 0

    For $i = 1 To $aFiles[0]

        _GUICtrlListView_InsertColumn($g_hListView, 0, $aFiles[Abs(($aFiles[0] * -1) + $i) + 1], 100)
        $aString = FileReadToArray($sDirPath & "\" & $aFiles[$i])
        If $i = 1 Then
            For $x = 0 To UBound($aString) - 1
                _GUICtrlListView_AddItem($g_hListView, $aString[$x])
            Next
        Else
            For $x = 0 To UBound($aString) - 1
                _GUICtrlListView_AddSubItem($g_hListView, $x, $aString[$x], $i - 1)
            Next
        EndIf

    Next



    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites

It could be a little tricky if the text files contain a different number of names

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $g_hListView

Example()

Func Example()
    Local $hGUI, $hImage
    $hGUI = GUICreate("Litview", 400, 300)
    Local $sDirPath = @scriptdir
    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, $LVS_EX_GRIDLINES)
    GUISetState(@SW_SHOW)

    Local $aFiles = _FileListToArray($sDirPath, "*.txt")
;~  _ArrayDisplay($aFiles)

    Local $n
    For $i = 1 To $aFiles[0]
         $tmp = _FileCountLines($sDirPath & "\" & $aFiles[$i])
         $n = ($n<$tmp) ? $tmp : $n
    Next
    For $i = 0 To $n
         _GUICtrlListView_AddItem($g_hListView, "")
   Next

    Local $aString = 0
    For $i = 1 To $aFiles[0]
         _GUICtrlListView_AddColumn($g_hListView, $aFiles[$i], 100)
          $aString = FileReadToArray($sDirPath & "\" & $aFiles[$i])
          If $i = 1 Then
                For $x = 0 To UBound($aString) - 1
                    _GUICtrlListView_SetItemText($g_hListView, $x, $aString[$x])
                Next
        Else
                 For $x = 0 To UBound($aString) - 1
                        _GUICtrlListView_AddSubItem($g_hListView, $x, $aString[$x], $i - 1)
                Next
        EndIf
    Next


    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites

@mikell  you have broken my script's copyright lol. Good ternary trick.

 

Saludos

Share this post


Link to post
Share on other sites

Holy shit guys, that's awesome!

Exactly what I wanted to achieve! Thank you very much.

_GUICtrlListView_AddColumn($g_hListView, $aFiles[$i], 100)

I see that this is the line that defines the names of the rows. Is it possible to hide the ".txt" part?

 

Saludos ;)

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