Sign in to follow this  
Followers 0
PlayHD

Write text file from listbox

6 posts in this topic

#1 ·  Posted (edited)

Hy..

1st to all i want to sorry for my english...

now... i have a little problem...

I have this program

Posted Image

I made a program to read all files from a directory and subdirectory, insert it all in a list box (list1) and if you press on first buton move selected string in listbox 2.

After i selected all files that you need, i select a text file and i want to save all strings from listbox2.

But here its the problem... the script down write me anything in my text file

#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <File.au3>
#include <WindowsConstants.au3>
$c = FileSelectFolder("Select folder","")
If $c = "" Then
Exit
EndIf
$form = GUICreate("Metin2Autopatcher by PlayHD files.txt creator",521,415,192,124)
$List1 = GUICtrlCreateList("", 8, 8, 209,357)
$List2 = GUICtrlCreateList("", 296, 8, 209, 357)
$Button1 = GUICtrlCreateButton(">", 224, 136, 59, 25)
$Button2 = GUICtrlCreateButton("All >", 224, 168, 59, 25)
$Button3 = GUICtrlCreateButton("< All", 224, 200, 59, 25)
$Button4 = GUICtrlCreateButton("<", 224, 232, 59, 25)
$Button5 = GUICtrlCreateButton("Write", 424, 376, 83, 25, $BS_DEFPUSHBUTTON)
$Button6 = GUICtrlCreateButton("Save as", 360, 376, 59, 25)
$Input1 = GUICtrlCreateInput(@DesktopDir&"files.txt", 8, 378, 345, 21)
$label = guictrlcreatelabel("Inserting files...",8,400,100,20)
GUICtrlSetState(-1,$GUI_DISABLE)
$arrays = _FileListToArrayEx($c, "*.*", 1, Default, True, True)
If @error Then
MsgBox(0,"No files to read...","Your selected folder is empty...")
Exit
EndIf
GUISetState(@SW_SHOW)
For $ir = 1 to $arrays[0] Step 1
_GUICtrlListBox_InsertString($List1,StringReplace($arrays[$ir],$c&"",""))
Next
GUICtrlSetData($label,"Ready :) ")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button1
   $cur_sel_1 = _GUICtrlListBox_GetCurSel($List1)
   If GUICtrlRead($List1) <> "" Then
    _GUICtrlListBox_InsertString($List2,GUICtrlRead($List1))
    _GUICtrlListBox_DeleteString($List1,_GUICtrlListBox_GetCurSel($List1))
   EndIf
   _GUICtrlListBox_SetCurSel($List1, $cur_sel_1)
  Case $Button2
   GUICtrlSetData($label,"Transfering...")
   $numb_of_el_1 = _GUICtrlListBox_GetCount($list1)
   For $ir = 1 to $numb_of_el_1 Step 1
    if _GUICtrlListBox_GetText($list1,0) <> "" Then
     _GUICtrlListBox_InsertString($list2,_GUICtrlListBox_GetText($list1,0))
     _GUICtrlListBox_DeleteString($list1,0)
    EndIf
   Next
   GUICtrlSetData($label,"Ready :)")
  Case $Button3
   GUICtrlSetData($label,"Transfering...")
   $numb_of_el_2 = _GUICtrlListBox_GetCount($list2)
   For $ir = 1 to $numb_of_el_2 Step 1
    if _GUICtrlListBox_GetText($list2,0) <> "" Then
     _GUICtrlListBox_InsertString($list1,_GUICtrlListBox_GetText($list2,0))
     _GUICtrlListBox_DeleteString($list2,0)
    EndIf
   Next
   GUICtrlSetData($label,"Ready :)")
  Case $Button4
   $cur_sel_2 = _GUICtrlListBox_GetCurSel($List2)
   If GUICtrlRead($List2) <> "" Then
    _GUICtrlListBox_InsertString($List1,GUICtrlRead($List2))
    _GUICtrlListBox_DeleteString($List2,_GUICtrlListBox_GetCurSel($List2))
   EndIf
   _GUICtrlListBox_SetCurSel($List2, $cur_sel_2)
  Case $Button6
   $ss_s = GUICtrlRead($Input1)
   $ss = FileSaveDialog("Save as : ",@DesktopDir,"All (*.*)")
   If @error Then
    $ss=$ss_s
   EndIf
   GUICtrlSetData($Input1,$ss)
   GUICtrlSetState($Input1,$GUI_DISABLE)
  Case $Button5
   GUICtrlSetData($label,"Writing...")
   If Not FileExists(GUICtrlRead($Input1)) Then
    _FileCreate(guictrlread($Input1))
   EndIf
   $file = FileOpen(GUICtrlRead($Input1))
   $numb_of_el_2 = _GUICtrlListBox_GetCount($list2)
   For $ir = 0 To $numb_of_el_2 Step 1
    If _GUICtrlListBox_GetText($list2,$ir) <> 0 Then
     FileWriteLine($file,_GUICtrlListBox_GetText($list2,$ir))
    EndIf
   Next
   GUICtrlSetData($label,"Thats all...")
   FileClose($file)
   $ans = MsgBox(68,"Succes","File saved as "&GUICtrlRead($Input1)&@LF&"You want to exit?")
   If $ans = 6 Then
    Exit
   EndIf
   GUICtrlSetData($label,"Ready :)")
EndSwitch
WEnd

;~ _ArrayDisplay($arrays)
Func _FileListToArrayEx($s_path, $s_mask = "*.*", $i_flag = 0, $s_exclude = -1, $f_recurse = True, $f_full_path = True)
    If FileExists($s_path) = 0 Then Return SetError(1, 1, 0)
    ; Strip trailing backslash, and add one after to make sure there's only one
    $s_path = StringRegExpReplace($s_path, "[/]+z", "") & ""
    ; Set all defaults
    If $s_mask = -1 Or $s_mask = Default Then $s_mask = "*.*"
    If $i_flag = -1 Or $i_flag = Default Then $i_flag = 0
    If $s_exclude = -1 Or $s_exclude = Default Then $s_exclude = ""
    ; Look for bad chars
    If StringRegExp($s_mask, "[/:><|]") Or StringRegExp($s_exclude, "[/:><|]") Then
        Return SetError(2, 2, 0)
    EndIf
    ; Strip leading spaces between semi colon delimiter
    $s_mask = StringRegExpReplace($s_mask, "s*;s*", ";")
    If $s_exclude Then $s_exclude = StringRegExpReplace($s_exclude, "s*;s*", ";")
    ; Confirm mask has something in it
    If StringStripWS($s_mask, 8) = "" Then Return SetError(2, 2, 0)
    If $i_flag < 0 Or $i_flag > 2 Then Return SetError(3, 3, 0)
    ; Validate and create path + mask params
    Local $a_split = StringSplit($s_mask, ";"), $s_hold_split = ""
    For $i = 1 To $a_split[0]
        If StringStripWS($a_split[$i], 8) = "" Then ContinueLoop
        If StringRegExp($a_split[$i], "^..*?..*?z") Then
            $a_split[$i] &= "*" & $a_split[$i]
        EndIf
        $s_hold_split &= '"' & $s_path & $a_split[$i] & '" '
    Next
    $s_hold_split = StringTrimRight($s_hold_split, 1)
    If $s_hold_split = "" Then $s_hold_split = '"' & $s_path & '*.*"'
    Local $i_pid, $s_stdout, $s_hold_out, $s_dir_file_only = "", $s_recurse = "/s "
    If $i_flag = 1 Then $s_dir_file_only = ":-d"
    If $i_flag = 2 Then $s_dir_file_only = ":D"
    If Not $f_recurse Then $s_recurse = ""
    $i_pid = Run(@ComSpec & " /c dir /b " & $s_recurse & "/a" & $s_dir_file_only & " " & $s_hold_split, "", @SW_HIDE, 4 + 2)
    While 1
        $s_stdout = StdoutRead($i_pid)
        If @error Then ExitLoop
        $s_hold_out &= $s_stdout
    WEnd
    $s_hold_out = StringRegExpReplace($s_hold_out, "v+z", "")
    If Not $s_hold_out Then Return SetError(4, 4, 0)
    ; Parse data and find matches based on flags
    Local $a_fsplit = StringSplit(StringStripCR($s_hold_out), @LF), $s_hold_ret
    $s_hold_out = ""
    If $s_exclude Then $s_exclude = StringReplace(StringReplace($s_exclude, "*", ".*?"), ";", "|")
    For $i = 1 To $a_fsplit[0]
        If $s_exclude And StringRegExp(StringRegExpReplace( _
            $a_fsplit[$i], "(.*?[/]+)*(.*?z)", "2"), "(?i)Q" & $s_exclude & "E") Then ContinueLoop
        If StringRegExp($a_fsplit[$i], "^w:[/]+") = 0 Then $a_fsplit[$i] = $s_path & $a_fsplit[$i]
        If $f_full_path Then
            $s_hold_ret &= $a_fsplit[$i] & Chr(1)
        Else
            $s_hold_ret &= StringRegExpReplace($a_fsplit[$i], "((?:.*?[/]+)*)(.*?z)", "$2") & Chr(1)
        EndIf
    Next
    $s_hold_ret = StringTrimRight($s_hold_ret, 1)
    If $s_hold_ret = "" Then Return SetError(5, 5, 0)
    Return StringSplit($s_hold_ret, Chr(1))
EndFunc

thanks in advance :)

Edited by PlayHD

Share this post


Link to post
Share on other sites



Hi PlayHD,

FileOpen() without any parameters opens a file in READ mode.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Check the return codes from FileOpen(). FileWriteLine() expects a second parameter - text. :)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Change this part of your Case statement for $Button5 to this:

Case $Button5
        GUICtrlSetData($label, "Writing...")
;       If Not FileExists(GUICtrlRead($Input1)) Then   <<<<<<<<<<< 
;           _FileCreate(GUICtrlRead($Input1))           This section isn't needed because you're opening the file correctly now.
;       EndIf                                          <<<<<<<<<<<
        $file = FileOpen(GUICtrlRead($Input1), 1) ; Open it in append mode
        $numb_of_el_2 = _GUICtrlListBox_GetCount($List2)
        For $ir = 0 To $numb_of_el_2 Step 1
            If _GUICtrlListBox_GetText($List2, $ir) <> "" Then ; Checking for 0 doesn't work with strings, check for an empty string instead
                FileWriteLine($file, _GUICtrlListBox_GetText($List2, $ir))
            EndIf
        Next

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Change this part of your Case statement for $Button5 to this:

Case $Button5
        GUICtrlSetData($label, "Writing...")
;       If Not FileExists(GUICtrlRead($Input1)) Then   <<<<<<<<<<<
;           _FileCreate(GUICtrlRead($Input1))          This section isn't needed because you're opening the file correctly now.
;       EndIf                                         <<<<<<<<<<<
        $file = FileOpen(GUICtrlRead($Input1), 1) ; Open it in append mode
        $numb_of_el_2 = _GUICtrlListBox_GetCount($List2)
        For $ir = 0 To $numb_of_el_2 Step 1
            If _GUICtrlListBox_GetText($List2, $ir) <> "" Then ; Checking for 0 doesn't work with strings, check for an empty string instead
                FileWriteLine($file, _GUICtrlListBox_GetText($List2, $ir))
            EndIf
        Next

thats it

Thanks all :)

see that all members of the board are very nice

Thanks again ;)

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