Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


prevent overwrite files when saving

Explorer ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

Hello guys

I have this snippet part of a script that save as TIFF version in a folder, How can i prevent overwrite is the File already exist in the folder, I would like to add a sequence numbering each time save the file. 

I want to keep the orginial name plus sequance numbering at the end.

 

I try to do this with increament increase but, but I think i am missing something like checking if the file exist if it does not add sequence numbering. i am stuck for now, hope someone can help.

Thanks in advance.

 

 

var count = [0];
incrementCount();
function incrementCount(){
   count++;
}

// saving location
var outputFolder = new File('/Dropbox/TIFF-ProPhoto/');
var Name = app.activeDocument.name.slice(0, -4);
var saveFile = File(outputFolder + '/' + Name + '_ProPhoto' + count + '.tiff');
saveTIFF(saveFile);

 

 

TOPICS
Actions and scripting, macOS, Windows

Views

178

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
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , Jul 25, 2021 Jul 25, 2021
fullName = String((aD = activeDocument).fullName) .replace(/(\d+|)(?=\.tif$)/i, function(_, v){return +v + 1}) optns = new TiffSaveOptions, aD.saveAs(File(fullName), optns)

Likes

Translate

Translate
Adobe Community Professional , Jul 25, 2021 Jul 25, 2021
If you look at Adobe Script "Image Processor.jsx" You will see how it generates uniquee output file names. /////////////////////////////////////////////////////////////////////////////// // Function: CreateUniqueFileName // Usage: Given a folder, filename, and extension, come up with a unique file name // using a numbering system // Input: string for folder, fileName, and extension, extension contains the "." // Return: string for the full path to the unique file //////////////////////////////...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

fullName = String((aD = activeDocument).fullName)
.replace(/(\d+|)(?=\.tif$)/i, function(_, v){return +v + 1})
optns = new TiffSaveOptions, aD.saveAs(File(fullName), optns)

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
community guidelines
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Your code

 

fullName = String((aD = activeDocument).fullName).replace(/(\d+|)(?=\.tif$)/i, function(_, v){return +v + 1})

 

seem to add  +1 to the current document name if the document name does not have a numeric suffix it start at 1.  The code does not test to see if a file by that name exists it will overwrite the file.  The code in the Images processors check to see if the file exists it will not return an existing file name. The images processore will save new file not overwrite existing files. Your code will overwrite files.

JJMack

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
community guidelines
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

The user did not say there will be files with greater numbers in a folder already to make the script to check their existence. If so, there's no point to make something over needed.

 

The logic I see refers to need of saving another files instead resaving each time the starting file. No sense there to start from 1, then go through 2 and 3, and omit the existing 4th to create the 5th instead. It gives you wrong order of versions. The first one would be suffixed of 4, then next ones with 1, 2, 3, 5, 6.

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
community guidelines
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Even if there are no previous files but the first design in a folder there will be files after using your script. If they use your script always save files versions  they can not edit a file in the middle of a sequences of saved  versions for editing the file would overwrite files when they use your script to save a version. Using a single step action I have posted to save a version with the Image Processor Pro Photoshop Plug-in script would save a new version not overwrite any previous saved version. You just assign a F Key short to save a version. The Image processor Pro will create new version of versions you edit. As you can see  I saved 3 version of mynewdesign and then saved three version of version 2. The order is clearly visible nothing is out of order there is a need for creating good version suffixes and a need checking for existence with good suffix sequences there should not be any name conflict but Murphy state if it can happen it will.,,,image.png

 

JJMack

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
community guidelines
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

That's not our concern to discuss what's better for user who didn't clearly state whether he wants to lose uneeded already remains of his previous project by overwriting them over or keep untouched. I'm not him, but if I wanted the script he described I wouldn't like some files were skipped to be saved with another number from the queue. When I continue what I do I open the last file I stopped the work at, or if i'm not satisfied with latest effect then I open some previous and let following items be overwritten. Alternatively I don't work in same folder, but when job is done I create new one and start over.

 

If he wanted something more than already got he would say that. If he'd found out later he didn't know he might want something else than described at start then he would ask for that additionally. You shouldn't care more of unsaid stuff. It's only matter of the user either the goal was to get complete script or just simple snippet to develop it further on his own.

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
community guidelines
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

All I'm trying to say here is in my opinion the Image Processor scripts versioning code work well. I do not see any potential problems with how its naming works.

JJMack

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
community guidelines
Adobe Community Professional ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

Image Processor is meant for broad scope of users, so it must cover different situations. The given script here is for individual task, so due to specific user expectation it works well too.

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
community guidelines
Adobe Community Professional ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

Yes your script code work very well. It is short and faster then the images processor code  well suited for users that know and understand  how it works.   It a good solution for to a knowledgeable users like yourself. You are fantastic when it come to scripting Photoshop. However, a novice user may run into issue  over writing files if they edit a file saved by you script and they use you script to save a new version. Your script  is in your words not for a broad scope of users. That is all my append was trying the add.

JJMack

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
community guidelines
Adobe Community Professional ,
Aug 14, 2021 Aug 14, 2021

Copy link to clipboard

Copied

LATEST

Loll, this script is neither for knowledgeable users, nor for novices. I have no idea why you start talking about others this script was not meant for? The given code was written for only one specific user, who didn't want it worked other way, at least did not precisely stated that.

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
community guidelines
Adobe Community Professional ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

If you look at Adobe Script "Image Processor.jsx" You will see how it generates uniquee output file names.

 

///////////////////////////////////////////////////////////////////////////////
// Function: CreateUniqueFileName
// Usage: Given a folder, filename, and extension, come up with a unique file name
// using a numbering system
// Input: string for folder, fileName, and extension, extension contains the "."
// Return: string for the full path to the unique file
///////////////////////////////////////////////////////////////////////////////
function CreateUniqueFileName( inFolder, inFileName, inExtension ) {
	inFileName = inFileName.replace(/[:\/\\*\?\"\<\>\|]/g, "_");  // '/\:*?"<>|' -> '_'
	var uniqueFileName = inFolder + inFileName + inExtension;
	var fileNumber = 1;
	while ( File( uniqueFileName ).exists ) {
		uniqueFileName = inFolder + inFileName + "_" + fileNumber + inExtension;
		fileNumber++;
	}
	return uniqueFileName;
}
JJMack

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
community guidelines