Highlighted

Saving file via script

Participant ,
Jul 03, 2020

Copy link to clipboard

Copied

Hi. I'm trying to modify a script so that it won't save over another file with the same name.

How can I create a time stamp, which I could save as a string and append to the new filename?

 

Thanks.

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

First, you check if the file exists. If it does, modify a name and save it again. Something like that.

(function() {
	try {

		var contents = 'Lorem ipsum';

		var pathToFile = '~/Desktop/test.js';
		var file = new File(pathToFile);

		if (!file.exists) {
			writeFile(file, contents);
		} else {
			var fileName = getFileName(file);
			var date = new Date();
			var extensions = getExtension(file);

			file = new File(file.parent.fsName + '/' + fileName + ' ' + date.toString() + '.' + extensions);

			writeFile(file, contents);
		}


	} catch (e) {
		alert(e.toString() + '\nScript File: ' + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +
			'\nFunction: ' + arguments.callee.name +
			'\nError on Line: ' + e.line.toString());
	}

	function getFileName(file) {
		return file.name.split('.').slice(0, -1).join('.');
	}

	function getExtension(file) {
		return file.name.split('.').pop();
	}

	function writeFile(file, contents, encoding, openMode) {
		var success;

		file = (file instanceof File) ? file : new File(file);
		encoding = encoding || 'UTF-8';
		openMode = openMode || 'w'; // 'a', 'e', 'r', 'w';

		file.encoding = encoding;
		file.open(openMode);
		success = file.write(contents);
		file.close();

		if (!success) {
			throw new Error('Unable to write file ' + file.fsName);
		}

		return file;
	}
})();
TOPICS
How to, Scripting

Views

59

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

Saving file via script

Participant ,
Jul 03, 2020

Copy link to clipboard

Copied

Hi. I'm trying to modify a script so that it won't save over another file with the same name.

How can I create a time stamp, which I could save as a string and append to the new filename?

 

Thanks.

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

First, you check if the file exists. If it does, modify a name and save it again. Something like that.

(function() {
	try {

		var contents = 'Lorem ipsum';

		var pathToFile = '~/Desktop/test.js';
		var file = new File(pathToFile);

		if (!file.exists) {
			writeFile(file, contents);
		} else {
			var fileName = getFileName(file);
			var date = new Date();
			var extensions = getExtension(file);

			file = new File(file.parent.fsName + '/' + fileName + ' ' + date.toString() + '.' + extensions);

			writeFile(file, contents);
		}


	} catch (e) {
		alert(e.toString() + '\nScript File: ' + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +
			'\nFunction: ' + arguments.callee.name +
			'\nError on Line: ' + e.line.toString());
	}

	function getFileName(file) {
		return file.name.split('.').slice(0, -1).join('.');
	}

	function getExtension(file) {
		return file.name.split('.').pop();
	}

	function writeFile(file, contents, encoding, openMode) {
		var success;

		file = (file instanceof File) ? file : new File(file);
		encoding = encoding || 'UTF-8';
		openMode = openMode || 'w'; // 'a', 'e', 'r', 'w';

		file.encoding = encoding;
		file.open(openMode);
		success = file.write(contents);
		file.close();

		if (!success) {
			throw new Error('Unable to write file ' + file.fsName);
		}

		return file;
	}
})();
TOPICS
How to, Scripting

Views

60

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
Jul 03, 2020 0
Adobe Community Professional ,
Jul 03, 2020

Copy link to clipboard

Copied

First, you check if the file exists. If it does, modify a name and save it again. Something like that.

(function() {
	try {

		var contents = 'Lorem ipsum';

		var pathToFile = '~/Desktop/test.js';
		var file = new File(pathToFile);

		if (!file.exists) {
			writeFile(file, contents);
		} else {
			var fileName = getFileName(file);
			var date = new Date();
			var extensions = getExtension(file);

			file = new File(file.parent.fsName + '/' + fileName + ' ' + date.toString() + '.' + extensions);

			writeFile(file, contents);
		}


	} catch (e) {
		alert(e.toString() + '\nScript File: ' + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +
			'\nFunction: ' + arguments.callee.name +
			'\nError on Line: ' + e.line.toString());
	}

	function getFileName(file) {
		return file.name.split('.').slice(0, -1).join('.');
	}

	function getExtension(file) {
		return file.name.split('.').pop();
	}

	function writeFile(file, contents, encoding, openMode) {
		var success;

		file = (file instanceof File) ? file : new File(file);
		encoding = encoding || 'UTF-8';
		openMode = openMode || 'w'; // 'a', 'e', 'r', 'w';

		file.encoding = encoding;
		file.open(openMode);
		success = file.write(contents);
		file.close();

		if (!success) {
			throw new Error('Unable to write file ' + file.fsName);
		}

		return file;
	}
})();

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...
Jul 03, 2020 0
Participant ,
Jul 03, 2020

Copy link to clipboard

Copied

Hey @Tomas_Sinkunas  - 

 

Thanks so much for that detailed reply and example.

I'm only a novice when it comes to coding and haven't done much for quite a while - and for now, I was just trying to make a quick modification of someone else's script since it didn't include any checking for existing files and simply overwrote them.

 

 

 var date = new Date();
 var time = date.getTime();
 var extraForName = time.toString();

 

 

This bit of code let me create an extra string to append to file name so they'll always be unique - and this will give me a chance to rename as desired.

 

I haven't done scripting in After Effects before. I tried saving your snippet as a .jsx file and launched it via File>Scripts>Run Script - but I get Error: Unable to write file G:Desktop\Desktop\test.js  Error line 49 (which says throw new Error("Unable to write file " + file.fsName)   Will save for later and look at your code more carefully to see if I can integrate and have a better long term solution.

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...
Jul 03, 2020 0