Skip to main content
Known Participant
October 21, 2010
Question

Want to replace content in existing text file

  • October 21, 2010
  • 1 reply
  • 1178 views

Hi All,

Below is the script to write text. It is running good but it adds text after existing text.

I want to save this text file in my selected folder and replace exsiting text as well as open that text file automatically in front of all my open application.

Please help.

Thanks in advance.

function write(text){

var myDoc=app.activeDocument;

var myTextFilePath=myDoc.filePath + "/" + "mytextfile" + ".txt";

var myTextFile = new File(myTextFilePath);

if ( myTextFile.exists )

{

myTextFile.open("e");

myTextFile.seek(0, 2);

}

else {

myTextFile.open("w");

}

myTextFile.write(text+"\r");

}

This topic has been closed for replies.

1 reply

Harbs.
Brainiac
October 21, 2010

First of all, you need to make sure you close the file after writing to it. (i.e. myTextFile.close()) Leaving files open is a great way to lose data...

If you want to open the file in the default application, use:

myTextFile.execute();

Harbs

tansk02Author
Known Participant
October 22, 2010

Thanks for answer.

It execute the file but it does not replace my existing text. It adds text after exsiting text.

I want remove old text and add new text whenever run myscript.

Please help

tansk02Author
Known Participant
October 29, 2010

Does this do what you want? This will overwrite the original content of mytextfile.txt

Function write(text){
     var myDoc=app.activeDocument;
     var myTextFilePath=myDoc.filePath + "/mytextfile.txt";
     var myTextFile = new File(myTextFilePath);
     myTextFile.open("w");
     myTextFile.write(text);
     myTextFile.close();
     myTextFile.execute();
}


Thanks for your answer.

This code is not what I need. Actully I have 4 onClick events in my scripts to generate report.

When I use this code it contains only one code and replace all codes

What I need is when I run my script it replace old report file and contains all the text which script generates.

Please help.