Using If tag exists use it else use original (not001)

Advanced Renamer forum
#1 : 16/07-22 01:55
Ross Linfoot
Ross Linfoot
Posts: 2
I am renaming the files using exif tool as the search - on some files the tag does not exist , so the rename is using a blank string and then appending a numeric value to make unique. How do I say use the TAG string if it exists else use the original name if the TAG is empty/missing.
example :
<ID3 Artist> - <ID3 Title>.<Ext>
filename originally is:
_Lake Of Fire.mp3

new filename would be :
Nirvana - Lake Of Fire.mp3

but if the <ID3 Title> is missing then it becomes :
001.mp3

I am looking to say if the tag <ID3 Title> is missing from the file , then keep the original name:
_Lake Of Fire.mp3

same with using exif - <ExifTool:Title>

Thanks for any help , or pointers to scrippting which may help


16/07-22 01:55
#2 : 16/07-22 11:13
David Lee
David Lee
Posts: 1125
You will need to use a script. Try this simple example:

if (title = app.parseTags("<ID3 Title>")) {
return app.parseTags("<ID3 Artist>") + ' - ' + title;
}

You will need a bit more code if you want to check EXIF tags as well or want to test the title and artist tags separately.


16/07-22 11:13
#3 : 21/07-22 01:16
Ross Linfoot
Ross Linfoot
Posts: 2
Reply to #2:
Thanks - that got it working :

The goal at the end of the day was that I had named my files by date when I stored them , and then again when I moved off an old pc. so I wanted to read through all images and find the earliest date/time and then rename all the files. I found a function which validated a date (the one from working out the age of a baby ). I then just wrote the logic to take 4 date values from the image (and the filename) and then run through a compare to find the oldest and then use that as the actual filename.

This helped me after doing a cleanup based on filenames - Thanks again for the help :

Here is the script I used:
// this first function already exists on this forum
function isValidDate(dateString) {
// First check for the pattern
if(!/^\d{1,4}\/\d{1,2}\/\d{2}$/.test(dateString))
return false;

// Parse the date parts to integers
var parts = dateString.split("/");
var day = parseInt(parts[2], 10);
var month = parseInt(parts[1], 10);
var year = parseInt(parts[0], 10);

// Check the ranges of month and year
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;

var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;

// Check the range of the day
return day > 0 && day <= monthLength[month - 1];
}
// new script for pulling the various dates
var rl_FileName = app.parseTags("<Name>");
var y_FN_DateTime = rl_FileName.substring(0,4)
var m_FN_DateTime = rl_FileName.substring(4,6)
var d_FN_DateTime = rl_FileName.substring(6,8)
var hour_FN_DateTime = rl_FileName.substring(9,11)
var min_FN_DateTime = rl_FileName.substring(11,13)
var sec_FN_DateTime = rl_FileName.substring(13,15)
var rl_FN_DateTime = y_FN_DateTime + '/' + m_FN_DateTime + '/' + d_FN_DateTime + '-' + hour_FN_DateTime + ':' + min_FN_DateTime + ':' + sec_FN_DateTime
var rl_FN_Date = y_FN_DateTime + '/' + m_FN_DateTime + '/' + d_FN_DateTime
rl_oFN = y_FN_DateTime + m_FN_DateTime + d_FN_DateTime + hour_FN_DateTime + min_FN_DateTime + sec_FN_DateTime

var rl_nFN = isValidDate(rl_FN_Date) ? rl_oFN : 'Unknown-Filename'

var rl_DTO = item.exifToolValue('DateTimeOriginal')
var rl_sDTO = rl_DTO.substring(0,4) + '/' + rl_DTO.substring(5,7) + '/' + rl_DTO.substring(8,10)
var rl_oDTO = rl_DTO.substring(0,4) + rl_DTO.substring(5,7) + rl_DTO.substring(8,10) + rl_DTO.substring(11,13) + rl_DTO.substring(14,16) + rl_DTO.substring(17,19)
var rl_nDTO = isValidDate(rl_sDTO) ? rl_oDTO : 'Unknown-NewDTO'

var rl_CD = item.exifToolValue('CreateDate')
var rl_sCD = rl_CD.substring(0,4) + '/' + rl_CD.substring(5,7) + '/' + rl_CD.substring(8,10)
var rl_oCD = rl_CD.substring(0,4) + rl_CD.substring(5,7) + rl_CD.substring(8,10) + rl_CD.substring(11,13) + rl_CD.substring(14,16) + rl_CD.substring(17,19)
var rl_nCD = isValidDate(rl_sCD) ? rl_oCD : 'Unknown-NewCD'

var rl_MD = item.exifToolValue('ModifyDate')
var rl_sMD = rl_MD.substring(0,4) + '/' + rl_MD.substring(5,7) + '/' + rl_MD.substring(8,10)
var rl_oMD = rl_MD.substring(0,4) + rl_MD.substring(5,7) + rl_MD.substring(8,10) + rl_MD.substring(11,13) + rl_MD.substring(14,16) + rl_MD.substring(17,19)
var rl_nMD = isValidDate(rl_sMD) ? rl_oMD : 'Unknown-NewMD'

var rl_FMD = item.exifToolValue('FileModifyDate')
var rl_sFMD = rl_FMD.substring(0,4) + '/' + rl_FMD.substring(5,7) + '/' + rl_FMD.substring(8,10)
var rl_oFMD = rl_FMD.substring(0,4) + rl_FMD.substring(5,7) + rl_FMD.substring(8,10) + rl_FMD.substring(11,13) + rl_FMD.substring(14,16) + rl_FMD.substring(17,19)
var rl_nFMD = isValidDate(rl_sFMD) ? rl_oFMD : 'Unknown-NewFMD'

var rl_fn1 = ( rl_nFN <= rl_nDTO ) ? rl_nFN : rl_nDTO //filename < DateTiemOriginal
var rl_fn2 = ( rl_fn1 <= rl_nCD ) ? rl_fn1 : rl_nCD //above < CreateDate (ie picture taken date)

var rl_fn3 = ( rl_fn2 <= rl_nMD ) ? rl_fn2 : rl_nMD
var rl_fn4 = ( rl_fn3 <= rl_nFMD ) ? rl_fn3 : rl_nFMD

var rl_final = rl_fn4.substring(0,8) + '_' + rl_fn4.substring(8,15)

return app.parseTags(rl_final) + rl_final ;


21/07-22 01:16