Rename Files Without Extension

I have an issue where I have recovered from a disk failure and now my image files have no extension. I would like to know how to rename the files with the correct extensions.


the last time I had this issue, I think it was @vikingOSX last time wrote a script... however the script does not run.


can anyone help...


Regards, Paul

Posted on May 21, 2020 11:23 AM

Reply
Question marked as Top-ranking reply

Posted on May 22, 2020 7:33 AM

Hi Paul,


The original post is here — for image validation on files with extensions.


The original script that I wrote last Fall was used to validate files with extensions to their actual image type. It won't work with files without extensions, and neither will the UNIX file command, or even Spotlight's mdls utility that thinks image files without extensions are just public.data.


There are no tools available in macOS that can explore the internal image data, and arrive at the accurate extension information — except the third-party exiftool which you indicated was installed last Fall. Am I correct that your current images without extensions are only camera RAW images that may be RAF, CR2, or DNG image types?


This morning, using the same image data as last Fall, I wrote and tested a very short (8 lines) Zsh script that recursively retrieves all subordinate images of a parent directory and uses exiftool to peek inside them for their FileType. This is also the uppercase extension from the first paragraph that should be assigned to the raw image, and I use that in the renaming. When tested against the same images as I used last Fall for your other solution, the renaming was 100% accurate using known raw files.


You can run this script from the Terminal after making it executable. I have a Test4 directory on my Desktop. Do not use tilde path shorthand, as Zsh doesn't expand tilde without additional help:


cd Desktop
chmod +x addext.zsh
./addext.zsh ./Test4
Script has completed.


Copy and paste the following into BBEdit (which you also installed last Fall), and then save on your Desktop as addext.zsh.


#!/bin/zsh

: <<'COMMENT'

Recursively drill into directory hierarchy for image files with no
extension. Exclude text files. Use exiftool to inspect the image
and extract FileType (extension), then rename with that extension.

Requires third-party exiftool installation.

Usage: addext.zsh /path/to/parent/directory

VikingOSX, 2020-05-22, Apple Support Communities, No warranties of any kind.

COMMENT

STARTDIR="$1:a"
setopt extended_glob
for f in ${STARTDIR}/**/*~*.txt(.N);
do
    # print "${f}"
    EXT=$(/usr/local/bin/exiftool -s3 "-FileType" "${f}")
    mv "${f}" "${f}.${EXT}"
done
echo "Script has completed."


Before:


After:




Similar questions

46 replies

May 21, 2020 12:10 PM in response to John Galt

There is one cloddy, but workable way to do it with John's script.


Put all of the images into one folder. Copy the folder to two duplicates. Use the script to change all of one to .raf, the next to .dng, and the last to .cr2 .


Now, starting with any one of the three folders, select all and press Command+O. Those named correctly (the .raf set for example), will open in their related app. Separate those out and delete the rest.


Use the known good set of .raf file names as a list to remove those files from the other two folders since you already know those are correct.


Open the remaining files in folder two all using .dng as the extension. Again, separate out those that opened and remove the same named items from the third folder. All the rest of the items in folder 3 should be .cr2 images.

May 21, 2020 5:54 PM in response to Paul1762

Yes. Do jpeg and tiff correctly correspond to those file types?


Do you happen to know which file type corresponds to octet-stream? Would .raf be the correct extension for it?


Some file types seem to have extensions while others don't, which means the input file names can't be anticipated. That complicates things. Would renaming a file FUJIFILM_X-T31861.RAF to FUJIFILM_X-T31861.RAF.raf (for example) be a problem?

May 22, 2020 8:05 AM in response to VikingOSX

Viking, as ever thank you so much... I will get on with it now...


I have updated the script, one question does the test need to go up front, or is it ok below?


#!/bin/zsh


: <<'COMMENT'


Recursively drill into directory hierarchy for image files with no

extension. Exclude text files. Use exiftool to inspect the image

and extract FileType (extension), then rename with that extension.


Requires third-party exiftool installation.


Usage: addext.zsh /path/to/parent/directory


VikingOSX, 2020-05-22, Apple Support Communities, No warranties of any kind.


COMMENT


STARTDIR="$1:a"

setopt extended_glob

for f in ${STARTDIR}/**/*~*.txt(.N);

do

# print "${f}"

EXT=$(/usr/local/bin/exiftool -s3 "-FileType" "${f}")

mv "${f}" "${f}.${EXT}"


: <<'COMMENT'

Errata. In case there are any images that already have an extension,

we need to test for that, and ignore them.

The following change provides that extra precaution to avoid redundantly

adding to an existing extension. Update this code segment in the

previously provided script:


COMMENT


EXT=$(/usr/local/bin/exiftool -s3 "-FileType" "${f}")

# skip those files that already have an extension

[[ ${f:e} ]] && continue

mv "${f}" "${f}.${EXT}"



done

echo "Script has completed."


May 22, 2020 8:55 AM in response to Paul1762

My errata was meant to be a update, not an appendage to the original script. Here is what you should have now:


#!/bin/zsh

: <<'COMMENT'

Recursively drill into directory hierarchy for image files with no
extension. Exclude text files. Use exiftool to inspect the image
and extract FileType (extension), then rename with that extension.

Requires third-party exiftool installation.

Usage: addext.zsh /path/to/parent/directory

VikingOSX, 2020-05-22, Apple Support Communities, No warranties of any kind.

COMMENT

STARTDIR="$1:a"
setopt extended_glob
for f in ${STARTDIR}/**/*~*.txt(.N);
do
    # print "${f}"
    EXT=$(/usr/local/bin/exiftool -s3 "-FileType" "${f}")
    # skip those files that already have an extension
    [[ ${f:e} ]] && continue
    mv "${f}" "${f}.${EXT}"
done
echo "Script has completed."



May 22, 2020 10:09 AM in response to VikingOSX

Viking,

so I have created the .zsh file as you said in BBEdit and save the file as addict.zsh on my Desktop. I have also downloaded the ExifTool.


the next item I am not should when you say create and run this script from the Terminal after making it executable.


cd Desktop

chmod +x addext.zsh

./addext.zsh ./Test4

Script has completed.


how is this done please...


Regards, Paul

May 22, 2020 3:07 PM in response to Paul1762

I created a new Automator application on both Mojave 10.14.6 and Catalina 10.15.4. Looks just like the last image I provided to you. Ran it on a folder with three sub-directories totaling 13 extensionless image files. On both operating systems, the image extensions were added in under 10 seconds — without any permissions or application access challenges. The selected parent folder was on my Desktop.


If your images are on a NAS, or mountable USB stick, you may not have write permissions there, but otherwise, and pending on the code in the Run Shell Script action, it should just work, though presently, it lacks an end of processing dialog.


Post a screen shot of your Run Shell Script action window expanded to show all of the code in it.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Rename Files Without Extension

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.