Notifications
Clear all

[Closed] Textfile – How to search and replace?

How do i find some text in a txt file and replace it with some other text?

plz help

6 Replies

Here’s a snippet to find the string, check out the Maxscript help for more info!

(
	clearListener()
	
	sFile = @"c:	est.txt"
	sString = "findme"
	
	fFile = openFile sFile
	
	do
	(
		fString = skipToString fFile sString
		if fString != undefined then print ("String: " + sString + " found at " + (filePos(fFile) as string))
	) 
	while (not eof fFile)
	
	close fFile
)

Hope this helps.

 lo1

or

fn replaceTxtInFile fileName oldTxt newTxt =
(
 local s = dotnetClass "System.IO.File"
 s.WriteAllText fileName (substituteString (s.ReadAllText fileName) oldTxt newTxt)
)

Unless your file is huge. If it is, don’t do this.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

this might be safer for big files:


 fn replaceTxtInFile fileName oldTxt newTxt =
 (
 	local s = dotnetClass "System.IO.File"
 	s.WriteAllText fileName ((s.ReadAllText fileName asdotnetobject:on).Replace oldTxt newTxt)
 )
 
 lo1

Meant to do that, forgot the syntax

Thanks a ton!

How big is a big file?

more than Gbyte i guess.

edited: more than max value of Int32 … which is 2,147,483,647

so… try to find a limit and let us know.