Skip to main content
Participant
May 8, 2021
Answered

Saving sequential file names of the same file to multiple files?

  • May 8, 2021
  • 4 replies
  • 5976 views

I have scanned in a lot of old pictures.  Most scans contain up to 5 pictures inside one jpg scanned file.

 

I made an action that if I highlight a section - I can run the action to have to crop/auto-adjust/etc/then save the file - but as we all know, the action will simply overwrite the same file name if it's in the action.  I toggled the dialog for that step but I'm ready for a script that saves the same file name with an appending sequential number or letter.  

 

Scan001.jpg turns into

Scan001-a.jpg

Scan001-b.jpg

Scan001-c.jpg

Scan001-d.jpg

Scan001-e.jpg

 

OR

Scan001-1.jpg

Scan001-2.jpg

Scan001-3.jpg

Scan001-4.jpg

Scan001-5.jpg

 

Something like that automatically by me simply highlighting and running my action again.  Anyone have a script for something like that?  Or another method to automate this madness?  It started as a small scanning project and now I have hundreds of scans... 

This topic has been closed for replies.
Correct answer Stephen Marsh

Here are some of the scripts from those "broken" links:

 

/*
automate save as or export with sequential file names
https://community.adobe.com/t5/photoshop/automate-save-as-or-export-with-sequential-file-names/td-p/9003836

2020 - Original script modified to remove the original filename from the sequential output file
https://forums.adobe.com/message/4453915#4453915
*/

#target photoshop

main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles("*.jpg").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 3);
    //var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg");
    var saveFile = File(savePath + "/" + Suffix + ".jpg");
    SaveJPEG(saveFile, 8); // JPEG compression level
}

function SaveJPEG(saveFile, jpegQuality) {
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = jpegQuality;
    activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop
main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    Name = Name.replace(/\d+$/, '');
    layerName = app.activeDocument.activeLayer.name.replace(/\.[^\.]+$/, '');
    combinedName = Name + "_" + layerName;
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles(combinedName + "*.psd").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 4);
    var saveFile = File(savePath + "/" + combinedName + "_" + Suffix + ".psd");
    SavePSD(saveFile);
}

function SavePSD(saveFile) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop
main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.activeLayer.name.replace(/\.[^\.]+$/, '');
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles(Name + "*.psd").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 4);
    var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");
    SavePSD(saveFile);
}

function SavePSD(saveFile) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.psd").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 4);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");

SavePSD(saveFile);

}

function SavePSD(saveFile){ 

psdSaveOptions = new PhotoshopSaveOptions(); 

psdSaveOptions.embedColorProfile = true; 

psdSaveOptions.alphaChannels = true;  

psdSaveOptions.layers = true;  

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE); 

};

function zeroPad(n, s) { 

   n = n.toString(); 

   while (n.length < s)  n = '0' + n; 

   return n; 

};

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
        activeDocument.saveAs(createUniqueFileName(f))
        activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.psd',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
       (o = new JPEGSaveOptions ()).quality = 12
       activeDocument.saveAs(createUniqueFileName(f), o)
       activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.jpg',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

try {
    while (1)
        {
        var _file = new File("C:\\Users\\KoolKATZDesigns\\Desktop\\Script Tests\\Names.txt");

        var number = 0;

        if (_file.exists) 
            {
            _file.open("r");
            number = Number(_file.read());
            
            if (_file.error) { alert("Params read error!", "Error", true); _file.close(); break; }

            _file.close();    
            }
                    
        ++number;

        _file.open("w");
        _file.write(number);

        if (_file.error) { alert("Params save error!", "Error", true); _file.close(); break; }
    
        _file.close();    

        var _name = "DOC_" + number;
            
        app.documents.add(UnitValue(1920, "px"), UnitValue(1080, "px"), 72, _name, NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

        break;
        }
    }
catch (e) { alert(e); }

 

4 replies

Legend
May 9, 2021
Try replacing the saveAs command in action with a call to this script. Set the path and quality of the jpg in the script yourself.
Michael Bullo
Community Expert
Community Expert
May 9, 2021

If you can't achieve exactly what you want in Photoshop, Adobe Bridge has a fantastic batch renaming process.

 

Change multiple file names with Batch Rename in Bridge
https://youtu.be/N7iIPcoGfjg

Erik5E80Author
Participant
May 9, 2021

Sadly most of these are either broken links due to adobe's changes over the years.  I was able to piece together some code that seems to work, but pretty tedious.  I can't imagine I'm the only one desiring this as a mainstream option in a product that's been out for decades.  But thank you for your help.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 10, 2021

Here are some of the scripts from those "broken" links:

 

/*
automate save as or export with sequential file names
https://community.adobe.com/t5/photoshop/automate-save-as-or-export-with-sequential-file-names/td-p/9003836

2020 - Original script modified to remove the original filename from the sequential output file
https://forums.adobe.com/message/4453915#4453915
*/

#target photoshop

main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles("*.jpg").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 3);
    //var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg");
    var saveFile = File(savePath + "/" + Suffix + ".jpg");
    SaveJPEG(saveFile, 8); // JPEG compression level
}

function SaveJPEG(saveFile, jpegQuality) {
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = jpegQuality;
    activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop
main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    Name = Name.replace(/\d+$/, '');
    layerName = app.activeDocument.activeLayer.name.replace(/\.[^\.]+$/, '');
    combinedName = Name + "_" + layerName;
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles(combinedName + "*.psd").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 4);
    var saveFile = File(savePath + "/" + combinedName + "_" + Suffix + ".psd");
    SavePSD(saveFile);
}

function SavePSD(saveFile) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop
main();

function main() {
    if (!documents.length) return;
    var Name = app.activeDocument.activeLayer.name.replace(/\.[^\.]+$/, '');
    try {
        var savePath = activeDocument.path;
    } catch (e) {
        alert("You must save this document first!");
    }
    var fileList = savePath.getFiles(Name + "*.psd").sort().reverse();
    var Suffix = 0;
    if (fileList.length) {
        Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
    }
    Suffix = zeroPad(Suffix + 1, 4);
    var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");
    SavePSD(saveFile);
}

function SavePSD(saveFile) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}

function zeroPad(n, s) {
    n = n.toString();
    while (n.length < s) n = '0' + n;
    return n;
}

 

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.psd").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 4);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");

SavePSD(saveFile);

}

function SavePSD(saveFile){ 

psdSaveOptions = new PhotoshopSaveOptions(); 

psdSaveOptions.embedColorProfile = true; 

psdSaveOptions.alphaChannels = true;  

psdSaveOptions.layers = true;  

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE); 

};

function zeroPad(n, s) { 

   n = n.toString(); 

   while (n.length < s)  n = '0' + n; 

   return n; 

};

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
        activeDocument.saveAs(createUniqueFileName(f))
        activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.psd',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
       (o = new JPEGSaveOptions ()).quality = 12
       activeDocument.saveAs(createUniqueFileName(f), o)
       activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.jpg',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

try {
    while (1)
        {
        var _file = new File("C:\\Users\\KoolKATZDesigns\\Desktop\\Script Tests\\Names.txt");

        var number = 0;

        if (_file.exists) 
            {
            _file.open("r");
            number = Number(_file.read());
            
            if (_file.error) { alert("Params read error!", "Error", true); _file.close(); break; }

            _file.close();    
            }
                    
        ++number;

        _file.open("w");
        _file.write(number);

        if (_file.error) { alert("Params save error!", "Error", true); _file.close(); break; }
    
        _file.close();    

        var _name = "DOC_" + number;
            
        app.documents.add(UnitValue(1920, "px"), UnitValue(1080, "px"), 72, _name, NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

        break;
        }
    }
catch (e) { alert(e); }

 

Legend
May 8, 2021

@Erik5E80 wrote:

save the file - but as we all know, the action will simply overwrite the same file name if it's in the action.   



If the action is recorded without changing the file name, then it will not change the file name. This action will save any of your files with its original name.
Erik5E80Author
Participant
May 8, 2021

I WANT to change the filename.  I want the action to use the original name but append a number after it as I crop and save each area of the picture.  So if the filename exists already, increment the number.  

JJMack
Community Expert
Community Expert
May 9, 2021

An action can not do that you need to use Photoshop scripting use logic to generate the file names you want to save.

JJMack