Skip to main content
wanokuni
Known Participant
July 25, 2021
Answered

prevent overwrite files when saving

  • July 25, 2021
  • 2 replies
  • 1832 views

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);

 

 

This topic has been closed for replies.
Correct answer JJMack

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;
}

2 replies

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
July 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
///////////////////////////////////////////////////////////////////////////////
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
Kukurykus
Legend
July 25, 2021
fullName = String((aD = activeDocument).fullName)
.replace(/(\d+|)(?=\.tif$)/i, function(_, v){return +v + 1})
optns = new TiffSaveOptions, aD.saveAs(File(fullName), optns)
JJMack
Community Expert
Community Expert
August 12, 2021

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
Kukurykus
Legend
August 12, 2021

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.