find and replace text
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Fangbeast.
hope it helps you
Fangles
Here is a replacement template that Franco did, he is a useful person to know:):)hello. how can i find a part of text which is always on the some
position and constant and replace it with another text.
for example: from position 10 to position 14 which is: "0000"
should be replace with "0600" (text).
Code: Select all
; String function Replace
; Replaces all occurrences of 'replacestring' with 'newstring'.
; Usage: Replace(String$,ReplaceString$,NewString$)
;-----------------------------------------------------------------------
Procedure$ Replace(S$,R$,N$)
While FindString(S$,R$,1)>0
S$=Left(S$,(FindString(S$,R$,1)-1))+N$+Right(S$,Len(S$)-(FindString(S$,R$,1)+Len(R$)-1))
Wend
ProcedureReturn S$
EndProcedure
;------------------------------------------------------------------------------
Text$ = Replace("Hello World World !","World","Franco")
MessageBox_(0,Text$,"Replace: All is OK",0)
Fangles
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Fangbeast.
This was easier for me
(Hope I didn't forget anything)
Fangles
This next step up will read an entire file for those strings and replace them each time it sees them, writing all lines to a new file regardless. I took this approach, rather than trying anything fancy (like opening an entire file in memory) because there are limits to string sizes here and it's too tricky for me to do it in binary mode.that's not bad .
but i have to open a file (C:\TEST.TXT)
find "0128AGK" and replace it with "5555AFP" and close the file.
This was easier for me

Code: Select all
-----------------------------------------------------------------------------
Procedure$ Replace(S$,R$,N$)
While FindString(S$,R$,1)>0 S$=Left(S$,(FindString(S$,R$,1)-1))+N$+Right(S$,Len(S$)-(FindString(S$,R$,1)+Len(R$)-1))
Wend
ProcedureReturn S$
EndProcedure
----------------------------------------------------------------------
IF OpenFile(0, C:\TEST.TXT) 0
IF OpenFile(1, C:\TEST.TXT.NEW)
WHILE EOF(0) 0
TEMP$ = READSTRING()
TEMP$ = Replace(TEMP$, "0128AGK, "5555AFP")
USEFILE(1)
WRITESTRINGN(TEMP$)
USEFILE(0)
WEND
ENDIF
ENDIF
CLOSEFILE(0)
CLOSEFILE(1)
Fangles
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
1.) Use Repeat... Until together with EOF()
2.) Use Readfile, not Openfile for the 1st file..
cya,
...Danilo
(registered PureBasic user)
1.) Use Repeat... Until together with EOF()
2.) Use Readfile, not Openfile for the 1st file..
Code: Select all
Procedure.s Replace(S$,R$,N$)
While FindString(S$,R$,1)>0
S$=Left(S$,(FindString(S$,R$,1)-1))+N$+Right(S$,Len(S$)-(FindString(S$,R$,1)+Len(R$)-1))
Wend
ProcedureReturn S$
EndProcedure
;----------------------------------------------------------------------
If ReadFile(0, "D:\TEST\DTA.TXT")
If OpenFile(1, "D:\TEST\DTA2.TXT")
Repeat
UseFile(0)
TEMP.s = ReadString()
TEMP.s = Replace (TEMP.s, "0128AGK", "5555AFP" )
UseFile(1)
WriteStringN(TEMP.s)
Until Eof(0) 0
EndIf
EndIf
Ende:
CloseFile(0)
CloseFile(1)
...Danilo
(registered PureBasic user)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Fangbeast.
Eaach system is different:):)
But, it was 2.am in the morning and my brain was hurting
Fangles
Using OpenFile here works quite fine for me (on this computer) and together with Usefile, PB doesn't mistake which is output and which is input. And I always use WOF at the top with while and wend.1.) Use Repeat... Until together with EOF()
2.) Use Readfile, not Openfile for the 1st file..
Eaach system is different:):)
But, it was 2.am in the morning and my brain was hurting

Fangles
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
>Using OpenFile here works quite fine for me (on this computer)
>and together with Usefile, PB doesn't mistake which is output
>and which is input.
Yes it should work, but it didnt work
for Ralf.
So i looked at this little piece
of code and it didnt work for me too.
(I think Openfile() worked before for me)
I changed the checks (IF OpenFile...)
and inserted 2 Console commands
(OpenConsole() at the beginning and
PrintN(TEMP.s) after ReadString())
After a ReadString(), the printed
String was empty. Hmm...
So i only changed the OpenFile()
in ReadFile() and the String
was read correctly this time.
1st Problem found. Corrected.
>And I always use WOF at the top with while and wend.
I found out that the program
cant find the EOF() if you use
it with While...Wend.
The program didnt stop (i can see the
CPUtime at every time on a performance
monitor on the right side of my desktop).
Hmm... weird stuff.
I let it run for a few seconds...
BANG!... it crashed my Win2000
so badly. Wow. Its hard to crash
Win2000 so hard.
So i changed the thing to use
Repeat and it worked.
Repeat checks after the last
String is read, so it works
fine.
>Each system is different:):)
Yes, but with Windows every system
should be the same. Its not.
You see this by using the Win-API.
It works on Win2000, doesnt work
on Win95 and makes strange colors
on WinXP....
Anyway, i like the WinAPI -
but its soooo big that its
sometimes not easy to find
what you need.
I use MSDN, search for a function
and it gives me 200 hits...
cya,
...Danilo
(registered PureBasic user)
>Using OpenFile here works quite fine for me (on this computer)
>and together with Usefile, PB doesn't mistake which is output
>and which is input.
Yes it should work, but it didnt work
for Ralf.
So i looked at this little piece
of code and it didnt work for me too.
(I think Openfile() worked before for me)
I changed the checks (IF OpenFile...)
and inserted 2 Console commands
(OpenConsole() at the beginning and
PrintN(TEMP.s) after ReadString())
After a ReadString(), the printed
String was empty. Hmm...
So i only changed the OpenFile()
in ReadFile() and the String
was read correctly this time.
1st Problem found. Corrected.
>And I always use WOF at the top with while and wend.
I found out that the program
cant find the EOF() if you use
it with While...Wend.
The program didnt stop (i can see the
CPUtime at every time on a performance
monitor on the right side of my desktop).
Hmm... weird stuff.
I let it run for a few seconds...
BANG!... it crashed my Win2000
so badly. Wow. Its hard to crash
Win2000 so hard.
So i changed the thing to use
Repeat and it worked.
Repeat checks after the last
String is read, so it works
fine.
>Each system is different:):)
Yes, but with Windows every system
should be the same. Its not.
You see this by using the Win-API.
It works on Win2000, doesnt work
on Win95 and makes strange colors
on WinXP....
Anyway, i like the WinAPI -
but its soooo big that its
sometimes not easy to find
what you need.
I use MSDN, search for a function
and it gives me 200 hits...

cya,
...Danilo
(registered PureBasic user)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
I also mailed this msg to Fred:
>>could it be a bug in openfile?
>
>could you send me some code which failed ?
>It's really strange, all seems to work well here..
The following code doesnt work:
If i change OpenFile() to ReadFile()
everything works fine.
I use Win2000, but looks like it
also doesnt work with other OS´s.
cya,
...Danilo
Edited by - Danilo on 30 December 2001 19:44:35
I also mailed this msg to Fred:
>>could it be a bug in openfile?
>
>could you send me some code which failed ?
>It's really strange, all seems to work well here..
The following code doesnt work:
Code: Select all
;----
OpenConsole()
If OpenFile(0, "C:\TEST.PB")
UseFile(0)
TEMP.s = ReadString()
PrintN(TEMP.s)
Delay(3000)
CloseFile(0)
EndIf
;----
everything works fine.
I use Win2000, but looks like it
also doesnt work with other OS´s.
cya,
...Danilo
Edited by - Danilo on 30 December 2001 19:44:35
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Fangbeast.
But, I remember this NOT working with the 2.60 release of PB. With the 2.7 release, it worked fine. I'm running Windows XP.
With PB 2.6, I had exactly the same problem as above.
Fangles
Danilo, I went back over my code for my cataloguer and tested it, still works using 2 OpenFile statements.Code: Select all
;---- OpenConsole() If OpenFile(0, "C:\TEST.PB") UseFile(0) TEMP.s = ReadString() PrintN(TEMP.s) Delay(3000) CloseFile(0) EndIf ;----
But, I remember this NOT working with the 2.60 release of PB. With the 2.7 release, it worked fine. I'm running Windows XP.
With PB 2.6, I had exactly the same problem as above.
Fangles
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
Its not my problem (i dont use the filestuff atm),
but it doesnt work here:
PureBasic v2.70, Win2000 SP2, dual PIII-1000 @ 39°C (atm)
with 1,5GB PC133, 260GB HD space, DVD+CDRW (SCSI onboard),
1.44MB floppy, 100Mbit Network onboard, SB Live!1024,
GeForce3 /64MB with 1600x1200x32 on 22"monitor
Havent tried it on the 2nd PC,
but maybe that helps...
...Danilo
(registered PureBasic user)
Its not my problem (i dont use the filestuff atm),
but it doesnt work here:
PureBasic v2.70, Win2000 SP2, dual PIII-1000 @ 39°C (atm)
with 1,5GB PC133, 260GB HD space, DVD+CDRW (SCSI onboard),
1.44MB floppy, 100Mbit Network onboard, SB Live!1024,
GeForce3 /64MB with 1600x1200x32 on 22"monitor
Havent tried it on the 2nd PC,
but maybe that helps...
...Danilo

(registered PureBasic user)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by wayne1.
Edited by - wayne1 on 31 December 2001 06:50:38
hello.
how can i find a part of text which is always on the some
position and constant and replace it with another text.
for example: from position 10 to position 14 which is: "0000"
should be replace with "0600" (text).
Code: Select all
;Requires two Librarys StringLibraryEx and FileEx
;Download from [url]http://www.reelmediaproductions.com/pb[/url]
;Simple only three lines (minus the test)but only good for small files right now
;Load entire file into a string, PB V2.7 accepts 5000 max bytes
;but V2.8 will be 64k then unlimited? for V3.0
If(FileSize("C:\TEST.TXT")>4999)
MessageRequester("Error","File size is " +Str(FileSize("C:\TEST.TXT"))+" bytes, that's too large!",0)
Else
MyFile$=LoadTextFile$("C:\TEST.TXT")
;Replace all the occurences of"0128AGK" with "5555AFP"
NewFile$=Replace$(MyFile$,"0128AGK" ,"5555AFP" )
;Save altered file (using a different file name in this case)
;Creates new file or rewrites the contents if it already exists
SaveTextFile_("C:\TEST2.TXT",NewFile$)
EndIf
Edited by - wayne1 on 31 December 2001 06:50:38