Sign in to follow this  
Followers 0
searchresult

from list to text file

12 posts in this topic

#1 ·  Posted (edited)

I have tried to get it work but just wont happen. Every time I tried something, I would never get an error, script works fine but the txt file is always empty.

#include <GUIConstants.au3>
#include <file.au3>
#include <GuiList.au3>
#include <date.au3>
#Include <GuiListView.au3>

$Form1 = GUICreate("Form1", 506, 240, 193, 115)
GUISetIcon("logo.ico")
$Button1 = GUICtrlCreateButton("Add", 8, 120, 99, 25, 0)
$Button2 = GUICtrlCreateButton("Remove", 8, 152, 99, 25, 0)
$Pic1 = GUICtrlCreatePic("logo.jpg", 8, 8, 100, 100, BitOR($SS_NOTIFY,$WS_GROUP))
$Button3 = GUICtrlCreateButton("Save", 8, 184, 99, 26, 0)
$List1 = GUICtrlCreateList("", 112, 8, 385, 201)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("Add", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Remove", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("Save", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuItem4
            Exit
        Case $MenuItem2
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $Button1
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $MenuItem3
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $Button2
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $MenuItem5
            ;- need code to put items from a list to a text file
        Case $Button3
            ;- need code to put items from a list to a text file
        EndSwitch
WEnd

Here is a code with need code part cause I don't know what to do.

Any ideas???

Edited by searchresult

Share this post


Link to post
Share on other sites



_GUICtrlListGetText () and FileWrite() should be the only 2 function you need to create such a feature. Do you have an attempt at doing this you'd like to post?

Share this post


Link to post
Share on other sites

_GUICtrlListGetText () and FileWrite() should be the only 2 function you need to create such a feature. Do you have an attempt at doing this you'd like to post?

#include <GUIConstants.au3>
#include <file.au3>
#include <GuiList.au3>
#include <date.au3>
#Include <GuiListView.au3>

$Form1 = GUICreate("Form1", 506, 240, 193, 115)
GUISetIcon("logo.ico")
$Button1 = GUICtrlCreateButton("Add", 8, 120, 99, 25, 0)
$Button2 = GUICtrlCreateButton("Remove", 8, 152, 99, 25, 0)
$Pic1 = GUICtrlCreatePic("logo.jpg", 8, 8, 100, 100, BitOR($SS_NOTIFY,$WS_GROUP))
$Button3 = GUICtrlCreateButton("Save", 8, 184, 99, 26, 0)
$List1 = GUICtrlCreateList("", 112, 8, 385, 201)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("Add", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Remove", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("Save", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuItem4
            Exit
        Case $MenuItem2
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $Button1
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $MenuItem3
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $Button2
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $MenuItem5
            SaveListView($List1, 'C:\listview_data.txt')
        Case $Button3
            ;- need code to put items from a list to a text file
        EndSwitch
WEnd

Func SaveListView($list, $file)
    $data = ''
    For $i = 0 To _GUICtrlListViewGetItemCount($list) - 1
        $data &= _GUICtrlListViewGetItemText($list, $i) & @CRLF
    Next
    $data = StringTrimRight($data,2)
    FileDelete($file)
    FileWrite($file, $data)
EndFunc

I have tried with this also no error works fine but the file is empty.

Share this post


Link to post
Share on other sites

#include <GUIConstants.au3>
#include <file.au3>
#include <GuiList.au3>
#include <date.au3>
#Include <GuiListView.au3>

$Form1 = GUICreate("Form1", 506, 240, 193, 115)
GUISetIcon("logo.ico")
$Button1 = GUICtrlCreateButton("Add", 8, 120, 99, 25, 0)
$Button2 = GUICtrlCreateButton("Remove", 8, 152, 99, 25, 0)
$Pic1 = GUICtrlCreatePic("logo.jpg", 8, 8, 100, 100, BitOR($SS_NOTIFY,$WS_GROUP))
$Button3 = GUICtrlCreateButton("Save", 8, 184, 99, 26, 0)
$List1 = GUICtrlCreateList("", 112, 8, 385, 201)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("Add", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Remove", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("Save", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuItem4
            Exit
        Case $MenuItem2
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $Button1
            $message = "Hold down Ctrl or Shift to choose multiple files."
            $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4 )
            _GUICtrlListAddItem($List1,$var)
        Case $MenuItem3
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $Button2
            $line =  _GUICtrlListSelectedIndex ($List1)
            _GUICtrlListDeleteItem ( $List1, $line )
        Case $MenuItem5
            SaveListView($List1, 'C:\listview_data.txt')
        Case $Button3
            ;- need code to put items from a list to a text file
        EndSwitch
WEnd

Func SaveListView($list, $file)
    $data = ''
    For $i = 0 To _GUICtrlListGetInfo($list) - 1
        $data &= _GUICtrlListGetText($list, $i) & @CRLF
    Next
    $data = StringTrimRight($data,2)
    FileDelete($file)
    FileWrite($file, $data)
EndFunc

Its a list not a listview

Share this post


Link to post
Share on other sites

Its a list not a listview

So what should be the solution?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here's my (working) version. I also fixed the problem with selecting multiple files.

#include <GUIConstants.au3>
#include <file.au3>
#include <GuiList.au3>
#include <date.au3>
#Include <GuiListView.au3>

$Form1 = GUICreate("Form1", 506, 240, 193, 115)
GUISetIcon("logo.ico")
$Button1 = GUICtrlCreateButton("Add", 8, 120, 99, 25, 0)
$Button2 = GUICtrlCreateButton("Remove", 8, 152, 99, 25, 0)
$Pic1 = GUICtrlCreatePic("logo.jpg", 8, 8, 100, 100, BitOR($SS_NOTIFY, $WS_GROUP))
$Button3 = GUICtrlCreateButton("Save", 8, 184, 99, 26, 0)
$List1 = GUICtrlCreateList("", 112, 8, 385, 201)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("Add", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Remove", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("Save", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuItem4
            Exit
        Case $MenuItem2
            addtolist()
        Case $Button1
            addtolist()
        Case $MenuItem3
            $line = _GUICtrlListSelectedIndex($List1)
            _GUICtrlListDeleteItem($List1, $line)
        Case $Button2
            $line = _GUICtrlListSelectedIndex($List1)
            _GUICtrlListDeleteItem($List1, $line)
        Case $MenuItem5
            savetofile()
        Case $Button3
            savetofile()
    EndSwitch
WEnd

Func savetofile()
    Local $data = ''
    $fn = FileSaveDialog('Saving to file...', @MyDocumentsDir, 'Text files (*.txt)', 18, 'filename.txt')
    $file = FileOpen($fn, 2)
    For $i = 0 To _GUICtrlListGetInfo($List1)
        $txt = _GUICtrlListGetText($List1, $i)
        If $txt = -1 Then ContinueLoop
        If $data = '' Then
            $data &= $txt
        Else
            $data &= @LF & $txt
        EndIf
    Next
    FileWrite($file, $data)
    FileClose($file)
EndFunc

Func addtolist()
    $message = "Hold down Ctrl or Shift to choose multiple files."
    $var = FileOpenDialog($message, @ProgramFilesDir & "\", "Executable files (*.exe)", 1 + 4)
    If StringInStr($var, '|') Then
        $fod = StringSplit($var, '|')
        For $i = 2 To UBound($fod) - 1
            _GUICtrlListAddItem($List1, $fod[1] & '\' & $fod[$i])
        Next
    Else
        _GUICtrlListAddItem($List1, $var)
    EndIf
EndFunc

edit - woops, stray underscore.

Edited by xcal

Share this post


Link to post
Share on other sites

my post had a workign version. i simply changed the GUICtrlListViewGetItemCount() and _GUICtrlListViewGetItemText() to their list equivalents.

Share this post


Link to post
Share on other sites

Here's my (working) version. I also fixed the problem with selecting multiple files.

edit - woops, stray underscore.

Thanks it is working fine but it must be saved without save dialog in a folder where the script is located...

Share this post


Link to post
Share on other sites

Thanks it is working fine but it must be saved without save dialog in a folder where the script is located...

So simply change $fn to hold the path to your file. Maybe adjust FileOpen's option, depending on what you want, too.

Share this post


Link to post
Share on other sites

#10 ·  Posted

So simply change $fn to hold the path to your file. Maybe adjust FileOpen's option, depending on what you want, too.

Thanks I will try

Share this post


Link to post
Share on other sites

That is working fine but when I open file everything is in the same line. I need it to be saved as in the list

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