Bulk Image Rename: Keep similarity

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
falcon-z
Posts: 4
Joined: 30 May 2016 23:29

Bulk Image Rename: Keep similarity

Post by falcon-z »

Hi, I have a directory with multiple photos of the pattern
ABC_1111.JPG
ABC_1112.JPG
ABC_1113.JPG
ABC_1113.NEF
ABC_1114.JPG
...

where sometimes files with the same filename but different extensions exist.

Now, I want to rename them from exif data (date, time) with a specific pattern and add a counter (and if possible, lowercase extensions), but files that had the same file name before should have it also afterwards, e.g.
20160130_Bla_001.jpg
20160130_Bla_002.jpg
20160130_Bla_003.jpg
20160130_Bla_003.nef <-- same file name as jpg
20160130_Bla_004.jpg
...

I found out how to rename files using time stamps and add counters, but failed to retain the filename similarity when adding counters... Is there any solution/script I could use?

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Bulk Image Rename: Keep similarity

Post by highend »

Can be easily scripted.
20160130_Bla_003.nef <-- same file name as jpg
So the name "ABC_1113.NEF" should be transformed to:
<exif date with yyyymmdd>_<stripped name of the old file>_<formatted counter>.<ext>
Resulting in:
20160130_ABC_003.nef
(and not: 20160130_Bla_003.nef)?

If that's the case:
- The format of the original name is always: <some name>_<some digits>.<extension>?
- How many files are in a folder at maximum?
- You have the necessary codecs installed so that you get the exif date from a .nef file?
To check that, select the .nef file and type this into the address bar: text property("WhenTaken");
Hit enter afterwards and confirm that you see a date in the text window
- What should happen if a file doesn't have an exif date?
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Bulk Image Rename: Keep similarity

Post by highend »

One way:

Code: Select all

    $files = listfolder(, , 1+4, <crlf>);
    $fileCount = gettoken($files, "count", <crlf>);
    $countLength = ($fileCount < 101) ? "000" : (($fileCount < 1001) ? "0000" : "00000"));

    $log = "";
    $count = 0;
    $lastFileNumber = "";
    setting "BackgroundFileOps", 0;
    foreach($file, $files, <crlf>) {
        $curFileNumber = regexreplace($file, "^(.*)_(\d+)\.(.*)$", "$2");
        if !($curFileNumber) { $log = $log . "No number part found, skipped: " . gpc($file, "file") . <crlf>; continue; }
        $exifDate = property("WhenTaken", $file);
        if !($exifDate) { $log = $log . "No exif date available, skipped: " . gpc($file, "file") . <crlf>; continue; }
        $strippedName = regexreplace($file, "^(.*)_(\d+)\.(.*)$", "$1");
        if (regexmatches(gettoken($strippedName, 1, "_"), "^\d{8}$")) { $log = $log . "Exif date already in name, skipped: " . gpc($file, "file") . <crlf>; continue; }
        if ($curFileNumber != $lastFileNumber) { $count++; }
        $lastFileNumber = $curFileNumber;
        renameitem(formatdate($exifDate, "yyyymmdd") . "_" . $strippedName . "_" . format($count, $countLength) . "." . recase(gpc($file, "ext"), "l"), $file);
    }
    if ($log) { text $log; }
It skips files:
- That already have an formatted exif date in front of the name (to not reprocess files that have already been renamed by using the script before)
- That don't match the structure (the number part before the extension)
- That don't have an exif date
One of my scripts helped you out? Please donate via Paypal

falcon-z
Posts: 4
Joined: 30 May 2016 23:29

Re: Bulk Image Rename: Keep similarity

Post by falcon-z »

Sorry for the late reply...
highend wrote:
20160130_Bla_003.nef <-- same file name as jpg
So the name "ABC_1113.NEF" should be transformed to:
<exif date with yyyymmdd>_<stripped name of the old file>_<formatted counter>.<ext>
Resulting in:
20160130_ABC_003.nef
(and not: 20160130_Bla_003.nef)?
Not quite, sorry for the bad explanation. First, I thought if there is any switch in the bulk rename dialog of xyplorer that renames only unique filenames, ignoring possible file extensions (such that a counter would not increase for multiple files with the same name). However, this doesn't seem to be the case, so scripting will probably the only way to solve this...

So, back to the procedure. I am looking for a script that loops through all jpgs and (after dropping the old filename) renames them to the following pattern (if possible it would change the extension to lowercase, but that is not so important):

Code: Select all

<datec yyyy-mm-dd hh-mm> some dummy text <#001>
I don't have any special codecs installed, so if there is no native way how to extract the date information from exif data, the date of creation would be also fine (although I would prefer the exif date).
So far, it works, even without scripting (but with xyplorer's inbuild batch rename). But now comes the tricky part: Whenever there is a file with the same name (but different extension), it should rename it in such a way , that it also has the same name afterwards. In terms of the example above, a nef-image with the same name would get a new name with the counter incremented by one compared to its matching jpg.

As a bonus (but not directly necessary): If there are nef-files that were not renamed yet (because there are no matching jpgs), the same procedure could be applied the the nef-pictures: Rename them according to the pattern and rename all files in this folder that have the same name (but different extension) in a similar way.

I hope the problem became more clearly? Thank you in advance

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Bulk Image Rename: Keep similarity

Post by highend »

In terms of the example above, a nef-image with the same name would get a new name with the counter incremented by one compared to its matching jpg.
And from the first post:
20160130_Bla_003.jpg
20160130_Bla_003.nef <-- same file name as jpg
That's exactly the opposite of what you're describing...
<datec yyyy-mm-dd hh-mm> some dummy text <#001>
"some dummy text"...

Just provide an example of 5 files with real file names (including the case with the same file name but different extension) and how the output should look like. No dummy content^^
One of my scripts helped you out? Please donate via Paypal

falcon-z
Posts: 4
Joined: 30 May 2016 23:29

Re: Bulk Image Rename: Keep similarity

Post by falcon-z »

if I apply the xyplorers bulk rename command

Code: Select all

<datec yyyy-mm-dd hh-mm> Holidays [Peter Muller] <#001>
to a folder with images and other files, I get

Code: Select all

IMG_1850.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 001.JPG
IMG_1851.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.JPG
IMG_1851.nef   -->    2016-06-02 11-06 Holidays [Peter Muller] 003.nef
IMG_1851.txt   -->    2016-06-02 11-06 Holidays [Peter Muller] 004.txt
IMG_1854.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 005.JPG
IMG_1855.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 006.JPG
but I would like to have

Code: Select all

IMG_1850.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 001.jpg
IMG_1851.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.jpg
IMG_1851.nef   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.nef
IMG_1851.txt   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.txt
IMG_1854.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 003.jpg
IMG_1855.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 004.jpg
in three preferred (increasing) stages of extras
  1. get date from date modified
  2. get date from jpg exif data
  3. get date also from nef exif data, if no jpg with the same name exists, e.g.

Code: Select all

IMG_1850.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 001.jpg
IMG_1851.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.jpg
IMG_1851.nef   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.nef
IMG_1851.txt   -->    2016-06-02 11-06 Holidays [Peter Muller] 002.txt
IMG_1854.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 003.jpg
IMG_1855.JPG   -->    2016-06-02 11-06 Holidays [Peter Muller] 004.jpg
IMG_1858.nef   -->    2016-06-02 11-06 Holidays [Peter Muller] 006.nef
IMG_1858.txt   -->    2016-06-02 11-06 Holidays [Peter Muller] 006.txt
I don't know much about the exif functionalities of xyplorer for jpgs or nefs, hence the three "levels" of functionality. In my last post, the quote
In terms of the example above, a nef-image with the same name would get a new name with the counter incremented by one compared to its matching jpg.
was referring to the default rename function by xyplorer as exemplary provided also in the first rename example (second code box) of this post. Sorry for the ambiguous passage.

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Bulk Image Rename: Keep similarity

Post by highend »

Ok, so:

1. You will enter the arbitrary name part for every folder you use the script for (in your example: Holidays [Peter Muller])
2. Date, name and counter are separated by a single space (not by an underscore)
3. You still want the exif date as the preferred date and only the modified one if the exif isn't available, correct?

To get the exif date from a .nef file you need to install a .nef codec, e.g.:
http://downloadcenter.nikonimglib.com/e ... sw/28.html
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Bulk Image Rename: Keep similarity

Post by highend »

Try this on a backup of your data (go into the folder where all files are and execute the script):

- You can define the delimiter ($delim) yourself. ATM it's a single space
- It'll ask for the arbitrary name part. If "<default>" isn't edited, the script will quit
- It'll prefer the exif date of a file if it can read it. If not, the modified date is taken
- If the counter doesn't change because a file has the same ID (the number part) as the last one
the date of the last file (it belongs to the same set) is taken. Otherwise a .jpg and a .txt file with the same number part (e.g.:
IMG_1851.JPG + IMG_1851.txt) would get different timestamped names -> Doesn't make much sense

Code: Select all

    $delim = " ";
    $files = listfolder(, , 1+4, <crlf>);
    $fileCount = gettoken($files, "count", <crlf>);
    $countLength = ($fileCount < 101) ? "000" : (($fileCount < 1001) ? "0000" : "00000"));

    $name = input("Enter the arbitrary name part...", , "<default>", , , 400, 150);
    if ($name LikeI "<default>") { end 1==1; }

    $log = "";
    $count = 0;
    $lastID = "";
    $lastDate = "";
    setting "BackgroundFileOps", 0;
    foreach($file, $files, <crlf>) {
        $curDate = property("WhenTaken", $file);
        if !($curDate) { $curDate = property("#date.m", $file); }
        $curID = regexreplace($file, "^(.*)_(\d+)\.(.*)$", "$2");
        if !($curID) { $log = $log . "No number part found, skipped: " . gpc($file, "file") . <crlf>; continue; }
        if ($curID == $lastID) { $curDate = $lastDate }
        else { $count++; }
        $lastID = $curID;
        $lastDate = $curDate;
        renameitem(formatdate($curDate, "yyyy-mm-dd hh-nn") . $delim . $name . $delim . format($count, $countLength) . "." . recase(gpc($file, "ext"), "l"), $file);
    }
    if ($log) { text $log; }
One of my scripts helped you out? Please donate via Paypal

falcon-z
Posts: 4
Joined: 30 May 2016 23:29

Re: Bulk Image Rename: Keep similarity

Post by falcon-z »

Thanks, seems to work perfectly!

Post Reply