Is it possible to create a text file from scratch?

New Here ,
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.7K

Likes

Translate

Translate

Report

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

1 Correct Answer

Advocate , Feb 16, 2017
4everJang Advocate , 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 luckJang

Likes

Translate

Translate
Jump to answer Jump to answer
Advocate ,
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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
funsekon AUTHOR LATEST
New Here ,
Feb 16, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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