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

Is it possible to create a text file from scratch?

New Here ,
Feb 16, 2017 Feb 16, 2017

Copy link to clipboard

Copied

I'm using a text file to keep a number that is used in controlled documents.

For example file contents can be: 17-154. So when we need to create a new controlled document it will be 17-155 and so forth.

All that works fine, but:

- What if the text file does not exist?

I would like to create a text file from scratch and write '17-001' to it...

Any ideas?

Thanks guys.

TOPICS
Scripting

Views

2.9K

Translate

Translate

Report

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

correct answers 1 Correct answer

Advocate , Feb 16, 2017 Feb 16, 2017

var oFile = new File( sFileName );

oFile.encoding = "UTF-8";

if( !oFile.open('w'){

     alert( "Cannot create file: " + sFileName );

     return false;

}

oFile.writeln( "Whatever you want to write to the file" );

oFile.close( );

If you want to append text to an existing file, use oFile.open('a') instead. Don't forget to close the file after writing to it.

Good luck

Jang

Votes

Translate

Translate
Advocate ,
Feb 16, 2017 Feb 16, 2017

Copy link to clipboard

Copied

var oFile = new File( sFileName );

oFile.encoding = "UTF-8";

if( !oFile.open('w'){

     alert( "Cannot create file: " + sFileName );

     return false;

}

oFile.writeln( "Whatever you want to write to the file" );

oFile.close( );

If you want to append text to an existing file, use oFile.open('a') instead. Don't forget to close the file after writing to it.

Good luck

Jang

Votes

Translate

Translate

Report

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 ,
Feb 16, 2017 Feb 16, 2017

Copy link to clipboard

Copied

LATEST

Thanks 4everJang​,

So, you actually open the file to create it...never would have guessed that...

Your code works great!

Have a great day!

f

Votes

Translate

Translate

Report

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