Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Want to replace content in existing text file

New Here ,
Oct 21, 2010 Oct 21, 2010

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");

}

TOPICS
Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 21, 2010 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 22, 2010 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 22, 2010 Oct 22, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2010 Oct 27, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 28, 2010 Oct 28, 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();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 29, 2010 Oct 29, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 29, 2010 Oct 29, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 01, 2010 Nov 01, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 01, 2010 Nov 01, 2010
LATEST

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines