Want to replace content in existing text file
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Read the file to a variable and then use the JS replace function. Then write what is returned fro the function back to the file.
Copy link to clipboard
Copied
Thanks for your reply. But I couldn't undertsand. Could you please breif me in simple way or you can add code in my script.
Actually script runs succesfully but doesn't replace existing file.

Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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.

Copy link to clipboard
Copied
The script I posted will do that.
Your text editor might not display the changes if you have the text file open, because it shows a cached version of the contents of the textfile.
Copy link to clipboard
Copied
Let me more specific. As you said, it replaces old contents in the text file. Yes it replaces old contents.
I use this script to store my text report. So I have created 4 onClicks event on my script and when I checked all checkboxes and run this script it gives only text from my one checked box onClick function out of 4. Becuase it replaces all 3 onClicks text. So I need when I run this script with my all checked boxes on. It should replaces old file but contains all the text from my all Checked boxes onClicks functions.
Thanks

Copy link to clipboard
Copied
Join the strings from all the selected checkboxes and then write the combined string to the file.
Something like this pseudocode:
var report = ((checkbox1.checked==true)? report1: "")+((checkbox2.checked==true)? report2: "")+((checkbox3.checked==true)? report3: "")+((checkbox4.checked==true)? report4: "")
writeToFile(report);

