Site logo

File - Folder

How to check if a file is already open

The Adobe JavaScript documentation has the following warning around opening files:
NOTE: Be careful about opening a file more than once. The operating system usually permits you to do so, but if you start writing to the file using two different File objects, you can destroy your data.

To tell if a File object is already open we can use the tell() method
Retrieves the current position as a byte offset from the start of the file.
Returns a number, the position index.

For an open file It returns: 0 (for new or empty file), or any number. If the file is not open, it returns -1.


function main() {
	var file = new File("~/Desktop/Log.txt");
	if (file.exists) {"e");, 2);
	else {"w");

	var tell = file.tell(); // 0 for new or empty file; any number for existing file
	tell = file.tell(); // -1 the file is closed