If and or not condition not working

Started by theprof, November 19, 2024, 07:14:21 PM

Previous topic - Next topic

theprof

This is in bash, basically, I want to find all files where the mime type does not match the extension:
exiftool -r \
    -if '(($MIMEType eq "image/jpeg" and $FileName !~ /\.(jpg|jpeg)$/i) or \
          ($MIMEType eq "image/png" and $FileName !~ /\.png$/i) or \
          ($MIMEType eq "image/gif" and $FileName !~ /\.gif$/i) or \
          ($MIMEType eq "image/bmp" and $FileName !~ /\.bmp$/i) or \
          ($MIMEType eq "image/tiff" and $FileName !~ /\.(tiff|tif)$/i) or \
          ($MIMEType eq "image/heif" and $FileName !~ /\.(heif|heic)$/i) or \
          ($MIMEType eq "image/webp" and $FileName !~ /\.webp$/i) or \
          ($MIMEType eq "video/mp4" and $FileName !~ /\.mp4$/i) or \
          ($MIMEType eq "video/quicktime" and $FileName !~ /\.mov$/i) or \
          ($MIMEType eq "video/x-msvideo" and $FileName !~ /\.avi$/i) or \
          ($MIMEType eq "video/x-matroska" and $FileName !~ /\.mkv$/i) or \
          ($MIMEType eq "video/webm" and $FileName !~ /\.webm$/i) or \
          ($MIMEType eq "video/x-flv" and $FileName !~ /\.flv$/i) or \
          ($MIMEType eq "video/mpeg" and $FileName !~ /\.(mpeg|mpg)$/i) or \
          ($MIMEType eq "video/3gpp" and $FileName !~ /\.3gp$/i) or \
          ($MIMEType eq "video/ogg" and $FileName !~ /\.ogv$/i) or \
          ($MIMEType eq "video/MP2T" and $FileName !~ /\.ts$/i) or \
          ($MIMEType eq "video/x-ms-wmv" and $FileName !~ /\.wmv$/i) or \
          ($MIMEType eq "video/x-m4v" and $FileName !~ /\.m4v$/i))' \
    -p '${FileName}: ${MIMEType}' "$dir"

StarGeek

How is it not working? What is the exact output?
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

theprof

Quote from: StarGeek on November 19, 2024, 08:39:04 PMHow is it not working? What is the exact output?

Actually, I want a better command:

exiftool -F -overwrite_original_in_place -api LargeFileSupport \
        -ext '*' \
        -FileOrder DateTimeOriginal -FileOrder CreateDate -FileOrder ModifyDate -FileOrder -FileSize -FileOrder FileModifyDate \
        -api QuickTimeUTC \
        -d "%Y_%m_%d_%H_%M_%S_%%.2nc.%%le" \
        '-AllDates<FileModifyDate#' \
        '-AllDates<${ModifyDate#;$_ = undef if $_ lt "1971"}' \
        '-AllDates<${DateTimeOriginal#;$_ = undef if $_ lt "1971"}' \
        '-AllDates<${CreateDate#;$_ = undef if $_ lt "1971"}' \
        '-FileName<FileModifyDate' \
        '-FileName<${ModifyDate;$_ = undef if $_ lt "1971"}' \
        '-FileName<${DateTimeOriginal;$_ = undef if $_ lt "1971"}' \
        '-FileName<${CreateDate;$_ = undef if $_ lt "1971"}' \
        -r "$dir_path"


I want to make it so that it adds the correct lowercase file extension here. I have a lot of JPEG files that have MOV file extension so I want to fix those.

StarGeek

Remove .%%le from the date format string.

Add .$FileTypeExtension

'-FileName<FileModifyDate.$FileTypeExtension' \
'-FileName<${ModifyDate;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \
'-FileName<${DateTimeOriginal;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \
'-FileName<${CreateDate;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

theprof

Quote from: StarGeek on November 19, 2024, 10:00:02 PMRemove .%%le from the date format string.

Add .$FileTypeExtension

'-FileName<FileModifyDate.$FileTypeExtension' \
'-FileName<${ModifyDate;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \
'-FileName<${DateTimeOriginal;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \
'-FileName<${CreateDate;$_ = undef if $_ lt "1971"}.$FileTypeExtension' \

Great that works!

One problem I get the error Error: Not a valid PNG (looks more like a BMP) even if I use -F and -m options. How do I get it to rename it? Or do you recommend that I manually examine each case where it might be different?

StarGeek

You'll get this error on other misnamed files. For example, a quick test resulted in
Error: Not a valid PNG (looks more like a JPEG)
So the fact that exiftool can't write to AllDates safely has more priority than the rename command.

One option would be to separate them into two separate commands. One to make sure the extensions are all correct and another to write the time stamp. This would be the solution that doesn't require any manual input, though it would take longer if you were running it on a lot of files.

Or you could use -efile option which would result in a list of these problem files. Then you could use the two separate commands on just these files, passing the error file back to exiftool with the -@ (Argfile) option. Though this does break down in part three, as after the rename, the error list no longer has the correct names.

Highly edited example of the latter
exiftool -efile ErrorList.txt '-FileName<FileModifyDate.$FileTypeExtension' '-AllDates<FileModifyDate#' /path/to/files/
exiftool '-FileName<FileModifyDate.$FileTypeExtension' -@ ErrorList.txt

Thinking about it, you could then edit the ErrorList.txt by removing all the extensions and replacing them with an asterisk. Example, file.jpg is actually a PNG. This gets listed in ErrorList.txt as file.jpg. Run the rename, and now it is file.png. Open the text file, search/replace ".jpg" with ".*". Now run the AllDates command.

Thinking further, the third step could be automated by using a command line Search/Replace like sed. On Windows, it looks like there's a PowerShell command to do this, or a sed port (see Msys2), or third party program such as Find And Replace Text <insert Beavis and Butt-Head laugh here>
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype