Forums > Javascript

Find and Replace in a txt file (xml file)

May 02 2006 | 12:25 pm


I?m new to max and javascript as well…

Is it possible with javascript to read a textfile and change there a certain string in a certain line?
I looked at the jsfiletester example. So I know how to read and write a file.
But how can I load the content of the txt file to a variable?

The best would be a find and replace object ;-)

cheers Flo

May 03 2006 | 10:42 pm

ok so far I am:

function writefile(s)
var f = new File(s,"write","TEXT");
var s1 = ‘test’;
var s2 = ‘this’;
var s3 = ‘text’;

if (f.isopen) {
post("writing string to file: " + s2 + "n");
} else {
post("could not create file: " + s + "n");

i want to write an XML file. i have already the content of it. so i just have to paste it into the variables. but not so easy as expected…
if i paste the XML file to the variable JS has problems with the newlines and shift rights in the XML…
I attached the XML file which is basically a MOTION2 file. So you can try to paste it to one of the variables…
I also tried to get away with the newlines of the XML in the text editor but it won?t help.
I still get some error code like this:
? error: js: jsfiletester.js: Javascript SyntaxError: unterminated string literal, line 26
source line: lurSamples>1

cheers FLO

May 04 2006 | 10:04 pm

sorry for the aweful selftalk topic ;-) but…
hey I?m very proud to present the find and replace script:

function rwlines(r,w,clip)
var r = new File(r);
var i,a,c;
var w = new File(w,"write","TEXT");
var s;
var clip;
var neu;

if (r.isopen){
while (a=r.readline()) { // returns a string
post("line[" + i + "]: " + a + "n");
if ( != -1)
{neu = a.replace(/, clip);
w.writestring(neu + "n");}
{w.writestring(a + "n");}
} else {
post("could not open file: " + s + "n");

the only thing I don?t know yet, is how to save the new file in a certain folder…
cheers FLO

Viewing 3 posts - 1 through 3 (of 3 total)

Forums > Javascript