Skip to main content
Known Participant
August 29, 2010
Answered

script to rename files that have similar names?

  • August 29, 2010
  • 1 reply
  • 2895 views

Hi, folks.

I have my  RAW files in one folder and my derivative images (e.g., PSDs, TIFFs,  JPEGs) in another.  The derivative files only share the last four digits  of the RAW file names (e.g., the RAW file is named 100826-14278.CR2 but  the JPEG version is only named 4278.JPG).  Also, the derivative files  have no metadata.

Is there a way to use a script to match all the  derivatives to the RAW files based on the last four digits, then change their file names  to match the RAW names.  And, if that's possible, can Bridge then  cascade the metadata minus the camera data EXIF from the RAW files to  the derivative files?

Thanks!

-Greg

P.S. My knowledge of scripting stops with cursive handwriting, so please be gentle.

This topic has been closed for replies.
Correct answer Paul Riggott

Hi, Paul--

Thanks for the script.  I hate even having to ask this but...what the heck do I do with it?  Do I copy and paste all that code into a folder somewhere?  Maybe the Startup Scripts folder (which is blank)?

Please excuse my ignorance.

-Greg


Copy and paste the script into Extendscript Toolkit
This utility can be found in the relevant folder:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities

Start Bridge - Edit - Preferences - Startup Scripts - Click "Reveal My Startup Scripts"
This will open the folder where the script is to be saved.
(It is just a plain text file with a .jsx extension)
Close and restart Bridge and accept the new script.

You will now see a new menu item "MetaData" select "Rename and Add Metadata" from the menu.
It will now give you the option to select the raw files folder and the derivative files folder, it will then do the rename and add the metadata (hopefully).

It might be best to set up a couple of test folders and see if it works ok.

1 reply

Paul Riggott
Inspiring
August 30, 2010

What version of Photoshop are you using?

Anon1066Author
Known Participant
August 30, 2010

Hi, Paul.  CS5.


Paul Riggott
Inspiring
August 30, 2010

Could you give this a test please Greg...


#target bridge  
if( BridgeTalk.appName == "bridge" ) { 
var newMenuData = new MenuElement( "menu", "Add_Metadata", "after Help", "myMetaData" );
var newDataCommand = new MenuElement( "command", "Rename and Add Metadata", "at the end of myMetaData" , "zxcv" );
}
newDataCommand.onSelect = function () {
   renamePutXMP();
   }
function renamePutXMP(){
var win = new Window('dialog','Rename and Add Metadata');
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.alignChildren="row";
win.g10 = win.add('group');
win.g10.orientation = "row";
win.title = win.g10.add('statictext',undefined,'Rename / Add Metadata');
win.title.helpTip="Compliments of Paul";
win.title.alignment="bottom";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.alignChildren="fill";
win.g5 =win.p1.add('group');
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'Raw Files');
win.g5.st1.preferredSize=[100,20];
win.g5.et1 = win.g5.add('edittext');
win.g5.et1.preferredSize=[250,20];
win.g5.et1.enabled=false;
win.g5.bu1 = win.g5.add('button',undefined,'Browse');
win.g10 =win.p1.add('group');
win.g10.spacing=10;
win.g10.st1 = win.g10.add('statictext',undefined,'Derivative Images');
win.g10.st1.preferredSize=[100,20];
win.g10.et1 = win.g10.add('edittext');
win.g10.et1.preferredSize=[250,20];
win.g10.et1.enabled=false;
win.g10.bu1 = win.g10.add('button',undefined,'Browse');
win.g15 =win.p1.add('group');
win.g15.orientation='row';
win.g15.alignment="top";
win.g15.spacing=10;
win.g15.bu1 = win.g15.add('button',undefined,'Process');
win.g15.bu1.preferredSize=[200,30];
win.g15.bu2 = win.g15.add('button',undefined,'Cancel');
win.g15.bu2.preferredSize=[200,30];
win.g5.bu1.onClick=function(){
     rawFolder = Folder.selectDialog("Please select the raw files folder",app.document.presentationPath);
     if(rawFolder !=null){
  win.g5.et1.text =  decodeURI(rawFolder.fsName);
  }
}

win.g10.bu1.onClick=function(){
     derivativeFolder = Folder.selectDialog("Please select the derivative files folder",app.document.presentationPath);
     if(derivativeFolder !=null){
  win.g10.et1.text =  decodeURI(derivativeFolder.fsName);
  }
}
win.g15.bu1.onClick=function(){
    if(win.g5.et1.text == ''){
        alert("No Raw folder selected!");
        return;
        }
    if(win.g10.et1.text == ''){
        alert("No Derivative folder selected!");
        return;
        }
    win.close(1);
    process(rawFolder,derivativeFolder);
}
win.center();
win.show();
}

function process(rawFolder,derivativeFolder){
var rawFiles = rawFolder.getFiles (/\.(cr2|crw)$/i);
if(!rawFiles.length){alert("There are no raw files to be processed!"); return;}
var derivativeFiles=derivativeFolder.getFiles (/\.(jpg|tif|psd)$/i);
if(!derivativeFiles.length){alert("There are no derivative files to be processed!"); return;}
derivativeFiles=[];
for (var a in rawFiles){
var Name = decodeURI(rawFiles.name).replace(/\.[^\.]+$/, '');
var fileMask = Name.match(/\d\d\d\d$/)+".*";
var destFiles = derivativeFolder.getFiles(fileMask);
for(var z in destFiles){
var Ext = decodeURI(destFiles.name).replace(/^.*\./,'');
   destFiles.rename(Name+"."+Ext);
    }
}
$.sleep(300);
for (var a in rawFiles){
var Name = decodeURI(rawFiles
.name).replace(/\.[^\.]+$/, '');
fileMask = Name + ".*";
destFiles = derivativeFolder.getFiles(fileMask);
for(var z in destFiles){
    if(decodeURI(destFiles.name).match(/thm$/i)) continue;
updateMetaData(rawFiles
,destFiles)
    }
}
function updateMetaData(sourceFile,destFile){
if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
sourceFile = new Thumbnail(File(sourceFile));
destFile = new Thumbnail(File(destFile));
var md = sourceFile.synchronousMetadata;
var source = new XMPMeta( md.serialize() );
var md2 = destFile.synchronousMetadata;
var dest = new XMPMeta( md2.serialize() );
try{
copySchema( source, dest,XMPConst.NS_CAMERA_RAW,["RawFileName"]); 
copySchema( source, dest,XMPConst.NS_EXIF,["PixelXDimension","PixelYDimension","ExposureTime","FNumber"]);
copySchema( source, dest,XMPConst.NS_DC,[]);
copySchema( source, dest,XMPConst.NS_XMP_RIGHTS,[]);
copySchema( source, dest,XMPConst.NS_IPTC_CORE,[]);
var updatedPacket = dest.serialize(XMPConst.SERIALIZE_OMIT_PACKET_WRAPPER | XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
destFile.metadata = new Metadata(updatedPacket);
app.document.refresh();
}catch(e){alert(e+" "+e.line);}
    }
}
function copySchema( source, dest, namespace, omitProps ) { 
var propIter = source.iterator(XMPConst.ITERATOR_JUST_CHILDREN | XMPConst.ITERATOR_JUST_LEAF_NAME, namespace, "" );
var prop = propIter.next();
var prefix = XMPMeta.getNamespacePrefix( namespace );
while(prop) {     
  var name = prop.path.substring( prefix.length );    
   if(omitProps != undefined) var copy = !contains( omitProps, name);
  if( copy ) {
            try{
   XMPUtils.duplicateSubtree( source, dest, namespace, prop.path,namespace, prop.path, 0 );       
            }catch(e){}
  }
  prop = propIter.next();
}        
}
function contains( arr, member ){
var r = false;
for( var i = 0; i < arr.length &! r; ++i  ) {
  r = arr == member;
}
return r;