Jump to content

Need help to save modified text


Go to solution Solved by Andreik,

Recommended Posts

 

I found this code a long time ago and i don't know hwo is the author:

I made some changes and would like to make "Text Viewer" load text files from a folder and modify if need.

First of all, I admit that I am not an expert in writing scripts and that is why I am looking for your help/advise here.

So I would like to use the 'SAVE' button to save the edited/modified text file from  _GUICtrlRichEdit_Create window.

How do i make it to work, help me please ...

#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiRichEdit.au3>

Local $gui010 = GUICreate('Text Viewer', 600, 520)
GUISetBkColor(0xE5E4E2)
$SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE)
_GUICtrlTreeView_SetTextColor($hTV, 0x000000)
_GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
_GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC)

Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 3)
_GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

$edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

local $FileChange = guictrlcreatedummy()

local $SourcePath = @ScriptDir & "\" 
local $files = filefindfirstfile($SourcePath & '*.txt'), $file

if $files = -1 then
    msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath)
    Exit
endif

while 1
    $file = FileFindNextFile($files)
    if @error then exitloop
    _guictrltreeview_add($hTV,0,$file,0,0)
wend

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        case $FileChange
            _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV))))
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $TVN_SELCHANGEDW
                    GUICtrlSendtodummy($FileChange)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   

 

Link to comment
Share on other sites

#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiRichEdit.au3>

Local $gui010 = GUICreate('Text Viewer', 600, 520)
GUISetBkColor(0xE5E4E2)
$SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE)
_GUICtrlTreeView_SetTextColor($hTV, 0x000000)
_GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
_GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC)

Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 3)
_GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

$edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

local $FileChange = guictrlcreatedummy()

local $SourcePath = @ScriptDir & "\"
local $files = filefindfirstfile($SourcePath & '*.txt'), $file

if $files = -1 then
    msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath)
    Exit
endif

while 1
    $file = FileFindNextFile($files)
    if @error then exitloop
    _guictrltreeview_add($hTV,0,$file,0,0)
wend

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            _GUICtrlRichEdit_Destroy($edt010)
            Exit
        case $FileChange
            _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV))))
        Case $SaveButton
            $sPath = FileSaveDialog('Save file', @ScriptDir, 'Text (*.txt)')
            If $sPath Then FileWrite($sPath, _GUICtrlRichEdit_GetText($edt010))
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $TVN_SELCHANGEDW
                    GUICtrlSendtodummy($FileChange)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

 

Andreik

Thanks! It works fine but i need another way to save txt files.
Sorry, I didn't describe the problem in detail.
I need  just save the same file without FileSaveDialog and after saving just dialog message  (something like "saved")

Another question: is it possible to add an action to open a text file by double-clicking from the Treeview?

Thank you !

Link to comment
Share on other sites

If you know that save path then replace FileSaveDialog() with your file path.

#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiRichEdit.au3>

Local $gui010 = GUICreate('Text Viewer', 600, 520)
GUISetBkColor(0xE5E4E2)
$SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE)
_GUICtrlTreeView_SetTextColor($hTV, 0x000000)
_GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
_GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC)

Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 3)
_GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

$edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

local $FileChange = guictrlcreatedummy()

local $SourcePath = @ScriptDir & "\"
local $files = filefindfirstfile($SourcePath & '*.txt'), $file

if $files = -1 then
    msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath)
    Exit
endif

while 1
    $file = FileFindNextFile($files)
    if @error then exitloop
    _guictrltreeview_add($hTV,0,$file,0,0)
wend

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            _GUICtrlRichEdit_Destroy($edt010)
            Exit
        case $FileChange
            _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV))))
        Case $SaveButton
            FileWrite("<Your path here>", _GUICtrlRichEdit_GetText($edt010))
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_DBLCLK
                    ShellExecute($SourcePath & _GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hTV)))
                Case $TVN_SELCHANGEDW
                    GUICtrlSendtodummy($FileChange)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

1 hour ago, Andreik said:

If you know that save path then replace FileSaveDialog() with your file path.

Can't add there any path because i have more than 100 txt files in the same location with different names.

I tried like this but it doesn't work

FileWrite("$file", _GUICtrlRichEdit_GetText($edt010))

A while ago did exactly the same action using AMS with Input and ListBox

text = Input.GetText("Input1");
tSelected = ListBox.GetSelected("ListBox1");
if tSelected then
TextFile.WriteFromString(ListBox.GetItemData("ListBox1", tSelected[1]), text, false);
end
Dialog.TimedMessage("TXT FILE", "Text file has been saved!", 2000, MB_ICONNONE);

and it worked well
Using  Autoit it seems to be more complicated.

Thank you Anyway

 

Link to comment
Share on other sites

32 minutes ago, Antivoyager said:

Can't add there any path because i have more than 100 txt files in the same location with different names.

That does not make much sense.  In any language (AMS included), you absolutely need to specify a path and filename.

Both can be implicit, but it has to be specified to Windows.  Filename can be random, or based on date-time, or other method, but you need to provide a filename.

Now when you go to that folder containing 100 txt files, what do you see as filename ?

 

Link to comment
Share on other sites

12 minutes ago, Nine said:

That does not make much sense.  In any language (AMS included), you absolutely need to specify a path and filename.

Both can be implicit, but it has to be specified to Windows.  Filename can be random, or based on date-time, or other method, but you need to provide a filename.

Now when you go to that folder containing 100 txt files, what do you see as filename ?

Yes,  I agree with you, it is necessary to assign a path and file name. However, how can I assign a *.txt file name to the code when I see hundreds of txt files in the folder, I can’t figure out.

Link to comment
Share on other sites

It's up to you to decide the path and the file name. If you don't really care about the file name you can generate a random file name and check if it not used already. But as @Nine said, it doesn't make any sense what are you talking. Tell us what do you really want to do.

When the words fail... music speaks.

Link to comment
Share on other sites

Posted (edited)
23 minutes ago, Andreik said:

It's up to you to decide the path and the file name. If you don't really care about the file name you can generate a random file name and check if it not used already. But as @Nine said, it doesn't make any sense what are you talking. Tell us what do you really want to do.

When you select some .txt file name from Treeview, add something and simple save changes from used file name.

 

tv.png

Edited by Antivoyager
Link to comment
Share on other sites

14 hours ago, Antivoyager said:

I made some changes and would like to make "Text Viewer" load text files from a folder and modify if need.

Notepad does that. Notepad++ is nicer to use.

1 hour ago, Antivoyager said:

Can't add there any path because i have more than 100 txt files in the same location with different names.

Save the old file adding a timestamp and save the new file with the original name to save/have both ?

1 hour ago, Antivoyager said:

A while ago did exactly the same action using AMS

what is AMS ? Would you give me a link ?

Edit: you all type faster than me 😓

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Solution
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiRichEdit.au3>

Global $sPath
Local $gui010 = GUICreate('Text Viewer', 600, 520)
GUISetBkColor(0xE5E4E2)
$SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE)
_GUICtrlTreeView_SetTextColor($hTV, 0x000000)
_GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
_GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC)

Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 3)
_GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

$edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

local $FileChange = guictrlcreatedummy()

local $SourcePath = @ScriptDir & "\"
local $files = filefindfirstfile($SourcePath & '*.txt'), $file

if $files = -1 then
    msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath)
    Exit
endif

while 1
    $file = FileFindNextFile($files)
    if @error then exitloop
    _guictrltreeview_add($hTV,0,$file,0,0)
wend

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            _GUICtrlRichEdit_Destroy($edt010)
            Exit
        case $FileChange
            $sPath = $sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV))
            _GUICtrlRichEdit_SetText($edt010, fileread($sPath))
        Case $SaveButton
            Local $hFile = FileOpen($sPath, 2)
            FileWrite($hFile, _GUICtrlRichEdit_GetText($edt010))
            FileClose($hFile)
            MsgBox(0, '', 'The file as been saved.')
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_DBLCLK
                    ShellExecute($SourcePath & _GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hTV)))
                Case $TVN_SELCHANGEDW
                    GUICtrlSendtodummy($FileChange)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

When the words fail... music speaks.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...