Skip to main content
February 16, 2017
Answered

Is it possible to create a text file from scratch?

  • February 16, 2017
  • 1 reply
  • 3109 views

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.

This topic has been closed for replies.
Correct answer 4everJang

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

1 reply

4everJang
4everJangCorrect answer
Legend
February 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

February 16, 2017

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