BTW... I should mention this code did not work in PB5.40, 5.62, 6.20 and didn't work until I ran it in PB6.21
Code: Select all
;{- Enumerations / DataSections
;{ Window
Enumeration
#Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
#Text_0
#Search
#Replace
#Text_5
#Text_6
#CheckBox_7
#Ok_button
#Cancel_Button
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
StandardFile$ = "C:\ "
Pattern$ = "Text (*.txt;*.bat)|*.txt;*.bat |PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
Pattern = 0
filename$ = OpenFileRequester("sed style find&replace",StandardFile$,Pattern$,Pattern)
If filename$ =""
End
EndIf
Debug filename$
If OpenWindow(#Window_0, 424, 193, 400, 170, "Search & Replace Criteria", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
TextGadget(#Text_0, 15, 10, 365, 35, "Similar to Linux 'sed' command; This will find and replace each occurance of your search term. Search critieria is case sensitive.")
StringGadget(#Search, 120, 55, 255, 25, "")
StringGadget(#Replace, 120, 85, 250, 20, "")
TextGadget(#Text_5, 35, 55, 70, 15, "Search")
TextGadget(#Text_6, 35, 85, 65, 15, "Replace")
CheckBoxGadget(#CheckBox_7, 70, 110, 240, 25, "Line begins with search term")
ButtonGadget(#Ok_button,140,140,50,30," OK ")
ButtonGadget(#Cancel_Button,230,140,70,30," Cancel ")
EndIf
Repeat
Event = WaitWindowEvent()
Select Event
; ///////////////////
Case #PB_Event_Gadget
Etype = EventType()
Select EventGadget()
Case #Ok_button
Debug "Want to process"
process = #True
Break
Case #Cancel_Button
CloseWindow(#Window_0)
End
EndSelect
; ////////////////////////
Case #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window_0
CloseWindow(#Window_0)
End
EndIf
EndSelect
ForEver
search.s = GetGadgetText(#Search)
replace.s = GetGadgetText(#Replace)
If GetGadgetState(#CheckBox_7)
Debug "Checked"
search = #CRLF$+Mid(search,3)
Debug search
replace = #CRLF$+replace
EndIf
If process
If OpenFile(0,filename$)
Debug "Opened file OK"
text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
s.s = ReplaceString(text,search,replace)
Debug Len(text)
FileSeek(0,0)
WriteString(0,s)
CloseFile(0)
EndIf
Debug Len(text)
If FindString(text,search) = 0
MessageRequester("Maybe Try Again?", "Your search term was not found in the file." + Chr(10) + "Try again and double check your entry.", #MB_OK|#MB_ICONWARNING)
EndIf
EndIf
End