Sign in to follow this  
Followers 0
sathish

FIND REPLACE FOR TEXT FILES

6 posts in this topic

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Sorry for the inconvenience

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Please suggest me, whether this task is possible or not????

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