Jump to content

FIND REPLACE FOR TEXT FILES


Recommended Posts

HERE is the script

#include <Misc.au3>
#include <File.au3>
#include <String.au3>
#include <GUIEdit.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $data, $original
Global $file

If $cmdLine[0] > 0 Then
    If $cmdLine[1] <> "" Then
        $file = $cmdLine[1]
    EndIf
EndIf

If $file <> "" Then
    FileOpen($file,0)
    If @error = -1 Then
        MsgBox(48,"Error","Could not open the file.")
    EndIf
    $data = FileRead($file)
    $original = $data
    If @error = 1 Then
        MsgBox(48,"Error","Could not read the file contents.")
    EndIf
EndIf
#EndRegion ### End Startup Checks ###


HotKeySet("^o","_Open")

#Region ### START Koda GUI section ### Form=
$frmTextEditor = GUICreate("Sample Text Editor (STE)", 620, 435, -1, -1)
$txtEdit = _GUICtrlEdit_Create($frmTextEditor,"", 0, 0, 620, 435, BitOr($ES_MULTILINE,$ES_WANTRETURN, $ES_AUTOVSCROLL))

GUICtrlSetResizing(-1,102)
$mnuFile = GUICtrlCreateMenu("&File")
$mnuOpen = GUICtrlCreateMenuItem("&Open", $mnuFile)
$mnuSave = GUICtrlCreateMenuItem("&Save", $mnuFile)
$mnuSaveAs = GUICtrlCreateMenuItem("Save As...", $mnuFile)
$div = GUICtrlCreateMenuItem("",$mnuFile)
$mnuExit = GUICtrlCreateMenuItem("&Exit", $mnuFile)
#EndRegion ### END Koda GUI section ###

_GUICtrlEdit_SetText($txtEdit,$data)
GUISetState()

#Region ### Main body of program ###
While 1
    If _IsPressed("11") = 1 And _IsPressed("53") = 1 Then
        _Save(_GUICtrlEdit_GetText($txtEdit))
    EndIf
    If _IsPressed("11") = 1 And _IsPressed("41") = 1 Then
        _GUICtrlEdit_SetSel($txtEdit,0,-1)
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Exit()
        Case $mnuOpen
            _Open()
        Case $mnuSave
            _Save(_GUICtrlEdit_GetText($txtEdit))
        Case $mnuSaveAs
            $file = FileSaveDialog("Select file.",@WorkingDir,"text files (*.txt) | All files (*.*)",16)
            _Save(_GUICtrlEdit_GetText($txtEdit))
        Case $mnuExit
            
    EndSwitch
    Sleep(20)
WEnd
#EndRegion ### End the main body ###

#Region ### Custom Functions ###
Func _Open()
    $file = FileOpenDialog("Open",@WorkingDir,"text (*.txt)| all (*.*)")
    If @error = 1 Then
        MsgBox(48,"Error", "Could not open file or no file selected.")
    Else
        $data = FileRead($file)
        _GUICtrlEdit_SetText($txtEdit,$data)
    EndIf
EndFunc

Func _Save($sData)
    FileClose($file)
    FileOpen($file,2)
    If @error = -1 Then
        MsgBox(48,"Error","File could not be written to.")
    EndIf
    FileWrite($file,$sData)
    If @error = 1 Then
        MsgBox(48,"Error","Could not write file contents.")
    EndIf
    $original = $sData
EndFunc

Func _Exit()
    If _GUICtrlEdit_GetText($txtEdit) <> $original Then
    $ask = MsgBox(35,"Save?","Save changes to file?")
        If $ask = 6 Then
            _Save(_GUICtrlEdit_GetText($txtEdit))
            Exit
        ElseIf $ask = 7 Then
            Exit
        Else
        
        EndIf
    Else
        Exit
    EndIf
EndFunc
#EndRegion ### End Custom Functions ###

I need to create a Custom FIND REPLACE button

Thanks in advance

Link to comment
Share on other sites

Don't open a new thread! Stick with the you opened a few hours ago!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Here is the attached text file

My requirement is to open the ss.txt in the editor and FIND REPLACE the text

Please check the attached image

In "FIND REF" button the text <ref>(.*?)</ref> have to be founded one by one and In the "APPLY-STYLE" button <refss>(.*?)</refs> should be replaced

It should be carried one by one

If the user clicks FIND REF, it should find first <ref>(.*?)</ref> and if the user clicks "APPLY-STYLE" button <refss>(.*?)</refs> should be replaced, if the user clicks FIND REF, without clicking APPLY-STYLE, it should not replace, instead of replacing it should find the next <ref>(.*?)</ref>

Sorry for my bad English, hope u guys understood my requirement

Thanks in advance

ss.txt

post-69004-0-25818300-1334914086_thumb.j

Link to comment
Share on other sites

Here is my updated code

#include <Misc.au3>
#include <File.au3>
#include <String.au3>
#include <GUIEdit.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $data, $original
Global $file

If $cmdLine[0] > 0 Then
    If $cmdLine[1] <> "" Then
        $file = $cmdLine[1]
    EndIf
EndIf

If $file <> "" Then
    FileOpen($file,0)
    If @error = -1 Then
        MsgBox(48,"Error","Could not open the file.")
    EndIf
    $data = FileRead($file)
    $original = $data
    If @error = 1 Then
        MsgBox(48,"Error","Could not read the file contents.")
    EndIf
EndIf
#EndRegion ### End Startup Checks ###


HotKeySet("^o","_Open")

#Region ### START Koda GUI section ### Form=
$frmTextEditor = GUICreate("Sample Text Editor (STE)", 720, 435, -1, -1)
$txtEdit = _GUICtrlEdit_Create($frmTextEditor,"", 0, 0, 720, 435, BitOr($ES_MULTILINE,$ES_WANTRETURN, $ES_AUTOVSCROLL))

GUICtrlSetResizing(-1,102)
$mnuFile = GUICtrlCreateMenu("&File")
$mnuOpen = GUICtrlCreateMenuItem("&Open", $mnuFile)
$mnuSave = GUICtrlCreateMenuItem("&Save", $mnuFile)
$mnuSaveAs = GUICtrlCreateMenuItem("Save As...", $mnuFile)
$div = GUICtrlCreateMenuItem("",$mnuFile)
$mnuExit = GUICtrlCreateMenuItem("&Exit", $mnuFile)


$mnuFile1 = GUICtrlCreateMenu("&Edit")
$mnuOpen1 = GUICtrlCreateMenuItem("&REF- STYLE APPLY", $mnuFile1)
$mnuSave1 = GUICtrlCreateMenuItem("&Save", $mnuFile1)
$mnuSaveAs1 = GUICtrlCreateMenuItem("Save As...", $mnuFile1)
$div1 = GUICtrlCreateMenuItem("",$mnuFile1)
$mnuExit1 = GUICtrlCreateMenuItem("&Exit", $mnuFile1)


#EndRegion ### END Koda GUI section ###

_GUICtrlEdit_SetText($txtEdit,$data)
GUISetState()

#Region ### Main body of program ###
While 1
    If _IsPressed("11") = 1 And _IsPressed("53") = 1 Then
        _Save(_GUICtrlEdit_GetText($txtEdit))
    EndIf
    If _IsPressed("11") = 1 And _IsPressed("41") = 1 Then
        _GUICtrlEdit_SetSel($txtEdit,0,-1)
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Exit()
        Case $mnuOpen
            _Open()
        Case $mnuSave
            _Save(_GUICtrlEdit_GetText($txtEdit))
        Case $mnuSaveAs
            $file = FileSaveDialog("Select file.",@WorkingDir,"text files (*.txt) | All files (*.*)",16)
            _Save(_GUICtrlEdit_GetText($txtEdit))
        Case $mnuExit
            
        Case $mnuOpen1
            #include <ButtonConstants.au3>
            #include <GUIConstantsEx.au3>
            #include <WindowsConstants.au3>
            
            #Region ### START Koda GUI section ### Form=
            $Form1 = GUICreate("REF-STYLE APPLIER", 417, 76, 225, 287)
            $Button1 = GUICtrlCreateButton("FIND REF", 40, 16, 147, 49, 0)
            $Button2 = GUICtrlCreateButton("APPLY-STYLE", 232, 14, 147, 49, 0)
            GUISetState(@SW_SHOW)
            #EndRegion ### END Koda GUI section ###
            
            While 1
            $nMsg = GUIGetMsg()
            Switch $nMsg
            Case $GUI_EVENT_CLOSE
            Exit
            
            
        Case $button1
            


    EndSwitch
WEnd

            

        
            
            
            
    EndSwitch
    Sleep(20)
WEnd
#EndRegion ### End the main body ###

#Region ### Custom Functions ###
Func _Open()
    $file = FileOpenDialog("Open",@WorkingDir,"text (*.txt)| all (*.*)")
    If @error = 1 Then
        MsgBox(48,"Error", "Could not open file or no file selected.")
    Else
        $data = FileRead($file)
        _GUICtrlEdit_SetText($txtEdit,$data)
    EndIf
EndFunc

Func _Save($sData)
    FileClose($file)
    FileOpen($file,2)
    If @error = -1 Then
        MsgBox(48,"Error","File could not be written to.")
    EndIf
    FileWrite($file,$sData)
    If @error = 1 Then
        MsgBox(48,"Error","Could not write file contents.")
    EndIf
    $original = $sData
EndFunc

Func _Exit()
    If _GUICtrlEdit_GetText($txtEdit) <> $original Then
    $ask = MsgBox(35,"Save?","Save changes to file?")
        If $ask = 6 Then
            _Save(_GUICtrlEdit_GetText($txtEdit))
            Exit
        ElseIf $ask = 7 Then
            Exit
        Else
        
        EndIf
    Else
        Exit
    EndIf
EndFunc
#EndRegion ### End Custom Functions ###

Now please help me in completing my task

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...