A Photo / Video Organisation Script (YEAR-MONTH-DAY-COUNTRY_STATE_CITY_UNIQUENUMBER)

Advanced Renamer forum
#1 : 20/11-20 20:03
Matt Gorner
Matt Gorner
Posts: 6
Hi All

I wrote a script to take my photo / video backups from my iPhone and organise them into the following structure:


YEAR (Folder)
YEAR-MONTH-DAY-COUNTRY_STATE_CITY_UNIQUENUMBER (Filename)


It also checks for incorrect Epoch dates (https://en.wikipedia.org/wiki/Unix_time) which I've had happen on some older iPhone backups.

The script will try and use the more reliable tags for the date first, in order:

<Img Year> = Date photo taken EXIF data

<ExifTool:DateTimeOriginal> = Date photo taken EXIF data (HEIC photos)

<ExifTool:MediaCreateDate> = Date media was created EXIF data

<ExifTool:TrackCreateDate> = Date media was created EXIF data

<ExifTool:DateCreated> = Date file was created

<Year Created> = Date file was created


I've commented the code so you should be able to re-structure it to suit your own preferred folder and filename structure, hope that helps someone out there!

Cheers
Matt

P.S. Improvements to the script are welcome!

---------------------------------------------------

/*

Photo Organisation Script
v1.0
Matt Gorner
20 November 2020

Organises photos / videos into the following structure:

YEAR (Folder)
YEAR-MONTH-DAY-COUNTRY_STATE_CITY_UNIQUENUMBER (Filename)


When finding the correct date, we try these tags in order of preference (most to least reliable):

<Img Year> = Date photo taken EXIF data

<ExifTool:DateTimeOriginal> = Date photo taken EXIF data (HEIC photos)

<ExifTool:MediaCreateDate> = Date media was created EXIF data

<ExifTool:TrackCreateDate> = Date media was created EXIF data

<ExifTool:DateCreated> = Date file was created

<Year Created> = Date file was created

*/


// Set unique numbering start value (1)
var numbering = app.parseTags('_<Inc Nr:1>');


// To build our final paths and filename
var folder;
var fileName;


// Start with the least preferred option as a fallback (as this can be wrong) but it's always available
var year = app.parseTags('<Year Created>');
var month = app.parseTags('<Month Created>');
var day = app.parseTags('<Day Created>');


// Try the Year Taken tag first
var imgYearTaken = app.parseTags('<Img Year>');


// Does it exist?
if( imgYearTaken )
{
// Yes, then the month and day must be there too
year = imgYearTaken;
month = app.parseTags('<Img Month:00>');
day = app.parseTags('<Img Day>');
}


// Sometimes the EXIF date can be wrong and shows the Epoch date (https://en.wikipedia.org/wiki/Unix_time) - had this happen with bad iPhone backups
if( year == "1969" )
{

// Looks like a bad date, attempt to get the photo EXIF data 'DateTimeOriginal' (Needed for HEIC files)
var exifToolDateTimeOriginal = app.parseTags('<ExifTool:DateTimeOriginal>');

if( exifToolDateTimeOriginal )
{
// We need to parse the ExifTool:DateTimeOriginal string to break out the year, month and day.

// ExifTool returns the following YYYY:MM:DD HH:MM:SS, once parsed by app.parseTags it becomes YYYY_MM_DD_HH_MM_SS
// We can use the string function .split to separate each entry into an array using the underscore as the delimiter

var DateTimeOriginalArray = exifToolDateTimeOriginal.split("_");

// Now we can grab all the parts we need (array indices start at 0)
year = DateTimeOriginalArray[0];
month = DateTimeOriginalArray[1];
day = DateTimeOriginalArray[2];
}

}


// The year is STILL bad, so let's keep trying other methods to find the correct date
if( year == "1969" )
{
// Try the media creation date
var exifToolMediaCreateDate = app.parseTags('<ExifTool:MediaCreateDate>');

if( exifToolMediaCreateDate )
{
// Parsing is the same as above
var MediaCreateDateArray = exifToolMediaCreateDate.split("_");

year = MediaCreateDateArray[0];
month = MediaCreateDateArray[1];
day = MediaCreateDateArray[2];
}
}


// STILL stuck in the past!
if( year == "1969" )
{
// Try track creation date
var exifToolTrackCreateDate = app.parseTags('<ExifTool:TrackCreateDate>');

if( exifToolTrackCreateDate )
{
var TrackCreateDateArray = exifToolTrackCreateDate.split("_");

year = TrackCreateDateArray[0];
month = TrackCreateDateArray[1];
day = TrackCreateDateArray[2];
}
}


// Nope, still wrong!
if( year == "1969" )
{
// Try profile creation date
var exifToolProfileDateTime = app.parseTags('<ExifTool:ProfileDateTime>');

if( exifToolProfileDateTime )
{
var ProfileDateTimeArray = exifToolProfileDateTime.split("_");

year = ProfileDateTimeArray[0];
month = ProfileDateTimeArray[1];
day = ProfileDateTimeArray[2];
}
}


// Last try using EXIFTool's date created before falling back to the Windows date created
if( year == "1969" )
{
// The only thing left to try is the date the file was created
var exifToolDateCreated = app.parseTags('<ExifTool:DateCreated>');

if( exifToolDateCreated )
{
// We need to parse the ExifTool:MediaCreateDate string to break out the year, month and day.
// ExifTool returns the following YYYY:MM:DD HH:MM:SS, once parsed by app.parseTags it becomes YYYY_MM_DD_HH_MM_SS
// We can use the string function .split to separate each entry into an array using the underscore as the delimiter
var DateCreatedArray = exifToolDateCreated.split("_");

// Now we can grab all the parts we need (array indices start at 0)
year = DateCreatedArray[0];
month = DateCreatedArray[1];
day = DateCreatedArray[2];
}
}


// Now we have our Year, Month and Day tags, let's append the GPS Data

// Does the file has GPS EXIF data?
if(item.gpsExists == true)
{
// Yes, so let's organise our files like so:
// Year (Folder)
// Year-Month-Day-State_City_Number

folder = year;
fileName = year + "-" + month + "-" + day + app.parseTags('-<GPS Country>_<GPS State>_<GPS City>') + numbering;

}else
{
// No GPS data, so let's organise our files like so:
// Year (Folder)
// Year-Month-Day_Number
folder = year;
fileName = year + "-" + month + "-" + day + numbering;
}


// When choosing an output folder in Advanced Renamer's GUI, it doesn't add a folder seperator on the end.
// If we were to just add our folder on the end of the output path, the files won't be placed into the correct folder

// So let's check the output path has a folder seperator on the end (/ or \) (user could have entered it manually)
// Get the last character of the item.newPath variable
var LastChar = item.newPath.substring(item.newPath.length - 1, item.newPath.length);

// Check for / or \
if(LastChar === "/" || LastChar === "\\")
{
// Yes, it did, so set our new item output path: OUTPUTPATH + FOLDER + \
item.newPath = item.newPath + folder + "\\";
}else
{
// No it didn't, so set our new item output path: OUTPUTPATH + \ + FOLDER + \
item.newPath = item.newPath + "\\" + folder + "\\";
}

// Pass back new filename
return fileName;


20/11-20 20:03
#2 : 20/11-20 20:04
Matt Gorner
Matt Gorner
Posts: 6
No clue why the post title on the main screen is all jacked up!


20/11-20 20:04