ExifTool Forum

ExifTool => Newbies => Topic started by: hammad9860 on June 21, 2021, 10:14:11 AM

Title: Recurse through files that start with a specific string
Post by: hammad9860 on June 21, 2021, 10:14:11 AM
I'm trying to rename some screenshots that are named in the following manner:

Screenshot (18).png
Screenshot (132).png
Screenshot (1408).png

to the new filename that is:

Screenshot_YYYYMMDD-HHMMSS.png

using this command:

exiftool -ext PNG "-filename<Screenshot_${FileModifyDate} %+.11f.%e" -d "%Y%m%d-%H%M%S" "*" -r

The command works fine but I wanted to make the command run only on files that start with "Screenshot (" string. So I modified the command and came up with this:

exiftool -ext PNG "-filename<Screenshot_${FileModifyDate} %+.11f.%e" -d "%Y%m%d-%H%M%S" "Screenshot (*.*" -r

But it returns "No matching files" error.

BTW: I have folder with sub-folders named by months name and year and there are no files in the parent directory but all in sub-folders. I couldn't find any thread talking about this or any help in the documentation page.

Any help would be appreciated, thanks.
Title: Re: Recurse through files that start with a specific string
Post by: StarGeek on June 21, 2021, 11:28:07 AM
Quote from: hammad9860 on June 21, 2021, 10:14:11 AM
exiftool -ext PNG "-filename<Screenshot_${FileModifyDate} %+.11f.%e" -d "%Y%m%d-%H%M%S" "*" -r

exiftool -ext PNG "-filename<Screenshot_${FileModifyDate} %+.11f.%e" -d "%Y%m%d-%H%M%S" "Screenshot (*.*" -r

Wildcards do not work with the -r (-recurse) option (https://exiftool.org/exiftool_pod.html#r-.--recurse), at least, not in the way you want here.  Read that link and Common Mistake #2 (https://exiftool.org/mistakes.html#M2)

What you'll want to use is the -if option (https://exiftool.org/exiftool_pod.html#if-NUM-EXPR).  Additionally, since your command doesn't require actually reading any of the embedded data, you can speed things up a bit with the -fast option (https://exiftool.org/exiftool_pod.html#fast-NUM).

Try this
exiftool -ext PNG -d "%Y%m%d-%H%M%S" -fast4 -r -if "$Filename=~/^Screenshot \(/i" "-filename<Screenshot_${FileModifyDate} %+.11f.%e"  .

This command does a case insensitive check to see if the filename starts with "Screenshot (".  The -fast4 option doesn't even open the file and just reads the file system data.  The dot is used to indicate the current directory.  Change that to the directory path if you want to run the command on a different directory than the current one.
Title: Re: Recurse through files that start with a specific string
Post by: Luuk2005 on June 21, 2021, 11:39:21 AM
StarGeek is fast like the the Flash, but since Im already did typed it, this what Im going to post..
Please try adding something like...    -if4 "$Filename =~ /^Screenshot \(\d+\)\.png$$/i"

The "i" after /regex/ matches the whole filename as case-insensitive, or to make the just the extension case-insensitive...
-if4 "$Filename =~ /^Screenshot \(\d+\)\.(?i)png$$/"

Edit: Im not noticed you were using $FileModifyDate, so StarGeeks -Fast4 conducts faster than my -if4 .