Notifications
Clear all
[Closed] Textfile – How to search and replace?
May 22, 2012 4:52 pm
How do i find some text in a txt file and replace it with some other text?
plz help
6 Replies
May 22, 2012 4:52 pm
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.
May 22, 2012 4:52 pm
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
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)
)
May 22, 2012 4:52 pm
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.