Rename folder based on datetaken tag of included files

Hello. Need help with a simple modification of lxp's script (here).
I want to rename folders according to {datetaken} of .CR2 files in it, but this script stops when there are no such files (e.g. there're only JPGs, which have no datetaken), i've got

"Error at line 11, position 13
'metadata.image.datetaken' is null or not an object (0x800a138f)"

it also stops (doesn't work) if there is only one image in a folder with no {datetaken} meta.
is there a simple way to solve ерш issue?

var fsu = DOpus.FSUtil();

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return false;
    var foundTag = false;
    var folderEnum = fsu.ReadDir(item, 'r'); // recursive
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        if (folderItem.metadata == 'image') {
            var newAlbum = folderItem.metadata.image.datetaken.Format("D#yyyy-MM-dd");
            foundTag = true;
            break;
        }
    }
    return foundTag ? newAlbum : false;
}

Have you tried testing if it's null? I'd expect that to work, at least.

if (folderItem.metadata.image == null) or similar.

unfortunately, I don't understand anything about writing scripts and I don't know where to insert this line

Try

var fsu = DOpus.FSUtil();

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return false;
    var foundTag = false;
    var folderEnum = fsu.ReadDir(item, 'r'); // recursive
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        if (folderItem.metadata == 'none') continue;
        if (folderItem.metadata.image.datetaken == null) continue;
        foundTag = true;
        break;
    }
    return foundTag ? folderItem.metadata.image.datetaken.Format("D#yyyy-MM-dd") : false;
}
1 Like

works ok. and, Dear, lxp, can you add some line to rename folder to created date if there were no datetaken at all, e.g. for folder with JPGs only, thank you in advance

Enjoy!

var fsu = DOpus.FSUtil();

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return false;
    var returnDate = item.create;
    var folderEnum = fsu.ReadDir(item, 'r'); // recursive
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        returnDate = folderItem.create;
        if (folderItem.metadata == 'none') continue;
        if (folderItem.metadata.image.datetaken == null) continue;
        returnDate = folderItem.metadata.image.datetaken;
        break;
    }
    return returnDate.Format('D#yyyy-MM-dd');
}
1 Like

Enjoying it. Thanx.
what's the meaning of this line:

 var folderEnum = fsu.ReadDir(item, 'r'); // recursive

it seams like it view into all sub folders, but i getting an error if there is one subfolder in renaming folder.
image

when i remove subfolder - rename is well

Yes, the script goes into all subfolders.

Try this.

var fsu = DOpus.FSUtil();

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return false;
    var returnDate = item.create;
    var folderEnum = fsu.ReadDir(item); // non recursive
    // var folderEnum = fsu.ReadDir(item, 'r'); // recursive
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        returnDate = folderItem.create;
        if (folderItem.metadata == 'none') continue;
        if (folderItem.metadata.image.datetaken == null) continue;
        returnDate = folderItem.metadata.image.datetaken;
        break;
    }
    folderEnum.Close();
    return returnDate.Format('D#yyyy-MM-dd');
}
1 Like

Very useful script, congratulations! And to preserve the original name of the folder and just insert datetaken at the end of it as it would be?

Example: Folder > Folder (datetaken)

Thank you!

I tried so hard until I got it. Now this script inserts, at the end of the name of the selected folder, the capture date of the photos it contains.

The script can be executed directly from a button on the toolbar, but in the Function field select the option Standard function (Opus or external), no script even though it is a script.

@disablenosel:dirs
Rename PATTERN * TO *
@script jscript

var fsu = DOpus.FSUtil();

function OnGetNewName(getNewNameData) {
    var item = getNewNameData.item;
    if (!item.is_dir) return false;
    var returnDate = item.create;
    var folderEnum = fsu.ReadDir(item); // no recursivo, para recursivo... var folderEnum = fsu.ReadDir(item, 'r');
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        returnDate = folderItem.create;
        if (folderItem.metadata == 'none') continue;
        if (folderItem.metadata.image.datetaken == null) continue;
        datetaken = folderItem.metadata.image.datetaken;
        break;
    }
    folderEnum.Close();
    var datetaken_format = datetaken.Format('D#yyyy-MM-dd');
    var result = item.name + " (" + datetaken_format + ")"; 
       
    return result;
}

Insert datetaken.dcf (1.8 KB)