Highlighted

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.

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

TOPICS
Scripting

Views

2.6K

Likes

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

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.

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

TOPICS
Scripting

Views

2.6K

Likes

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
Feb 16, 2017 0
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
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 16, 2017 0
funsekon 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
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 16, 2017 0