Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v1.0 | Add formatted DateTimeOriginal exif tag and file group counter. Author: glambany@gmail.com
- const GROUP_CNT = true; // Enable the incrementing number by file group. Each group is of all files with same filename but different extention.
- const CNT_START = 1; // Value from where the group counter start counting
- const CNT_STEP = 1; // Counter incrementing steps.
- const CNT_DIGIT_NB = 3; // Number of digits, padded with zeros if necessary, that the counter number has
- const DTO = true; // Enable adding Datetimeoriginal to the filename
- const DTO_LOOKAROUND = true; // Enable the datetimeoriginal LOOKAROUND feature. It looks for a datetimeoriginal in other files around (1 up or down) if the current file hasn't any
- const KEEP_FILEN = true; // Enable keeping original filename in addition to the new stuff
- // Filename sep.: Date separator, Date prefix separator, Group counter prefix separator
- const DATE_SEP = "-", DATE_PREFIX = "_", CNT_PREFIX = "_";
- const NEWNAME_FOR_CNT = false; // Check the new name the files have (modified by previous methods) to detect group of files for the counter. If disabled, use the original filename regardless of what was modified of it
- // Start of the stuff you shouldn't touch, unless you know what you are doing of course
- var final_filename = "";
- // first, try to rename if the file has it's own datetimeoriginal
- if ( get_datetimeoriginal(index) != null ) {
- final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
- // then, try to find a file around that has a datetimeoriginal, and use it
- else if ( DTO_LOOKAROUND ){
- if ( index != 0 && get_datetimeoriginal(index - 1, DATE_SEP) != null && app.getItem(index - 1).name == item.name ) {
- final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index - 1, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
- else if ( index != (app.itemcount - 1) && get_datetimeoriginal(index + 1, DATE_SEP) != null && app.getItem(index + 1).name == item.name ) {
- final_filename = en(item.newBasename + DATE_PREFIX, KEEP_FILEN) + en(get_datetimeoriginal(index + 1, DATE_SEP), DTO) + en(en(CNT_PREFIX, DTO) + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT);}
- // can't find a datetimeoriginal around, keep the original filename
- else { final_filename = item.newBasename + en(CNT_PREFIX + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT); }
- }
- // file doesn't have a datetimeoriginal, and the LOOKAROUND is off, so, keep the original filename
- else { final_filename = item.newBasename + en(CNT_PREFIX + zpad(String(counter + CNT_START), CNT_DIGIT_NB), GROUP_CNT); }
- // Make sure we aren't at the last file
- if ( index != (app.itemcount - 1) ){
- // Increase the counter if the next file has a different name
- if (NEWNAME_FOR_CNT) {
- if( app.getItem(index + 1).newBasename != item.newBasename ){
- counter+= CNT_STEP;}
- }
- else {
- if( app.getItem(index + 1).name != item.name ){
- counter+= CNT_STEP;}
- }
- }
- return final_filename;
- // function to get datetimeoriginal from the file in the list with the given index.
- function get_datetimeoriginal(idx, sep) {
- var DTO = "";
- var aDate = [];
- //regular expression to extract date from datetimeoriginal (which is a date and a time)
- var reDateTimeSplit = /(\d{4}):(\d{2}):(\d{2})\s(\d{2}):(\d{2}):(\d{2})/;
- //extract datetimeoriginal from file
- DTO = app.getItem(idx).exifToolValue("DateTimeOriginal");
- if ( DTO.length != 0 ) {
- aDate = reDateTimeSplit.exec(DTO);
- return aDate[1] + sep + aDate[2] + sep + aDate[3]
- }
- else{ return null }
- }
- // Function that add leading zeros to any string
- function zpad(str, nb_digits)
- {
- if ( str.length < nb_digits )
- { return (Array(nb_digits - str.length + 1).join('0') + str); }
- else
- { return str; }
- }
- // return the string if enabled
- function en(str, enabled){
- if (enabled) {return str}
- else { return "" }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement