Inserting text at end of filename, but before the extension

Started by mcfibb, February 09, 2022, 12:24:33 PM

Previous topic - Next topic

mcfibb

Hi there -- I've searched high and low for the answer across ExifTools forum and other interweb pages. This is tied to a Google Takeout export issue.

I need to rename a bunch of JSON files that are missing the corresponding file's extension. A normal "matched" JSON/JPG pair would look like:

FileNumber1.JPG
Filenumber1.JPG.json

Unfortunately, when Google Takeout exported all my photos, some had ridiculously long filenames, so the corresponding json files were truncated and subsequently lacked the photo's filetype (i.e. missing from the json filename), like this:

FileNumber1_is_a_really_long_filename_for_whatever_reason.JPG
FileNumber1_is_a_really_long_filename_for_whatever_r.json

Now, when I run an open-source photo/json metadata matching program, it misses all these files because it can't match them.

Here's my question: what is the line of ExifTool code that will allow me to find any json file missing the JPG extension, and then insert "JPG" before the ".json" extension?

(I'm already set for the preceding necessary step of shortening all filename lengths to the same amount (e.g. 30 characters, which is more than enough).

Thanks for the help, and sincere apologies if this has been answered elsewhere on the forum (I really tried to find it!)

- McFibb

EDIT: Working in macOS.

StarGeek

First and foremost, Google did not remove any metadata from your files.  If you made no changes on the Google website, then your files still have the data from when you uploaded them and you do not need to "fix" your files.  The only data that was lost would have been the file system time stamps, FileCreateDate and FileModifyDate, which will always be lost when uploading a file to a cloud based server.

Otherwise, the answers can be found in this thread.

The first step is to rename the json files to correctly match the images.  This command will move the (#) part of the filename
exiftool -ext json -r -if '$Filename=~/(\.[^.]+)(\(\d+\)).json$$/i'  '-Filename<${Filename;s/(\.[^.]+)(\(\d+\)).json$/$2$1.json/}' /path/to/Takeout/

To fix the truncated json filenames, you'll have to rename the image files.  This command will rename the images to no more than 30 characters.
exiftool -ext jpg "-Filename=%30f.%e" /path/to/files/

Hopefully, there aren't any collisions for files reduced to 30 characters.  If so, you'll probably have to do those manually.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mcfibb

Hi StarGeek -- thanks for helping out. Definitely get that Google didn't remove any metadata. It's those FileCreated/Modified dates that are giving me trouble, and subsequently, the json data helps me get that back.

I've leaned heavily on that previous thread, and it certainly helped me when I needed to move those duplicate filenames as indicated by parenthesis and a given number (#). I've also sorted out the shortened filenames as well. What that thread didn't answer, and why I posted a new thread here, is that I need to re-add the photo's filetype extension into the json filename, but before the extension ".json". When I make this change manually, then run the open-source matching program, it goes off without a hitch. Unfortunately, I have thousands of remainder files that I need to do this with.

My problem is a bit different than the previous thread and the code you suggested: I need to modify the json filename from "FileName1_is_really_long_for_some_r.json" to "FileName1_is_really_long_for_some_r.JPG.json".

Does this make sense? To beat a dead horse:

Baseline Unmatched Pair:
FileName1_is_really_long_for_some_reason.JPG
FileName1_is_really_long_for_some_r.json

Unmatched Pair, after filename truncation:
FileName1_is_really_long_for_some_r.JPG
FileName1_is_really_long_for_some_r.json

MATCHED Pair:
FileName1_is_really_long_for_some_r.JPG
FileName1_is_really_long_for_some_r.JPG.json

Thanks!


mcfibb

Beating a dead horse, continued:

In layman's terms, here's the code I'm looking for:

If the filename of a JPG photo matches the filename of a json file that is missing ".JPG" in the filename , then insert ".JPG" before ".json" of that json file.

:)

StarGeek

Ah, I see the problem.  One way to fix it for just these files would be to change the %F.json in the original command to %f.json and run that separately.  The %F token includes the extension, the %f does not.  A second run will give errors on the files that are less than 30 characters, but that can be ignored.

Otherwise, using a more recent version of exiftool, try this.  I haven't tested it to make sure, though.
exiftool -ext json -if5 'length($Basename)==30' '-Filename=%f.jpg.json' /path/to/files/

Quote from: mcfibb on February 09, 2022, 01:14:53 PMDefinitely get that Google didn't remove any metadata. It's those FileCreated/Modified dates that are giving me trouble, and subsequently, the json data helps me get that back.

If these are photos from a camera that might included an embedded date (not screenshots or snapchat/FB pictures), you might first try copying that date from the file.
exiftool '-FileModifyDate<DateTimeOriginal' '-FileCreateDate<DateTimeOriginal' /path/to/files/
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mcfibb


mcfibb

That's it! A combination between all four lines of code in this post did the trick!

Thank you!

mcfibb

Actually -- one more thing. What is would the generic/regex code be for:

If a file with the file extension of ".jpg" has a filename that matches another file's filename, but with the file extension ".json".... THEN blah blah blah code code code.

I have some json files that correspond to JPG and others that correspond to PNG, etc, and the only way to match them together is by identical filename. The suggested code in the previous post is a nice "hatchet" method (which I'm using for some folders), but I also need the "scalpel" approach too!

Thanks!

StarGeek

I'd have to see a example, but in the -TagsFromFile %d%f.%e.json construct should cover it once the above alterations are made.  This looks for a file in the same directory %d with the same base filename %f and extension %e and adds the dot between the filename and the extension and appends the .json.  If you have
/path/File1.png
/path/File2.jpg
and
/path/File1.png.json
/path/File2.jpg.json
then the above format would copy date from the matching json file.

Further alterations to these variables, such as the %30f.%e construct above, can be done.  Details can be found under the Advanced features section of the -w (-TextOut) option.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

astroniu

Quote from: StarGeek on February 09, 2022, 03:37:59 PMAh, I see the problem.  One way to fix it for just these files would be to change the %F.json in the original command to %f.json and run that separately.  The %F token includes the extension, the %f does not.  A second run will give errors on the files that are less than 30 characters, but that can be ignored.

Otherwise, using a more recent version of exiftool, try this.  I haven't tested it to make sure, though.
exiftool -ext json -if5 'length($Basename)==30' '-Filename=%f.jpg.json' /path/to/files/

Sorry for bumping up this old thread. However, I've been trying to get this command to work for so long, but I keep getting this message:

Error: File not found - '-Filename=%f.jpg.json'

I thought it might be the extra "5" after "-if," but it doesn't work even after I removed it. I was able to truncate the JPG file names, so now the JPG and JSON files have the same name. However, trying to add .jpg back to the JSON file name just wouldn't work. Please help!

Phil Harvey

What command shell are you using?  And what ExifTool version?

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

astroniu

Hi Phil,

I'm really new to this. I just downloaded the windows executable (exiftool-12.73.zip), moved it to C:\Windows, and then use CMD to execute commands.

StarGeek

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

astroniu