Tuxedo <
[email protected]>:
I use a script to rename image files copied from a camera to include the >EXIF generated date into each filename.
The filenames originally appear as follows::
DSC00071.JPG
DSC00075.JPG
DSC00086.JPG
DSC00093.JPG
DSC00116.JPG
DSC00133.JPG
After running the script, they appear as:
20190531_083305_DSC00071.jpg
20190531_083429_DSC00075.jpg
20190531_092253_DSC00086.jpg
20190531_092643_DSC00093.jpg
20190531_093211_DSC00116.jpg
20190531_095200_DSC00133.jpg
Nothing wrong with the procedure, it works great, but sometimes I run it >twice on a batch of files by mistake, ending up with the following:
20190531_083305_20190531_083305_DSC00071.jpg >20190531_083429_20190531_083429_DSC00075.jpg >20190531_092253_20190531_092253_DSC00086.jpg >20190531_092643_20190531_092643_DSC00093.jpg >20190531_093211_20190531_093211_DSC00116.jpg >20190531_095200_20190531_095200_DSC00133.jpg
(... plus thousands more.)
Whenever it happens I erroneously prepend the date string and underscores to >previously processed files, i.e. the 16 characters of each filename as >above.
… and if by mistake, the script is run once more, there will be file
names containing a triple of identical date info…
How can I remove the 16 characters in each filename, by renaming all files >in the current working directory to retain only the last 28 last characters?
But so I don't rename filenames that may still exist in the same directory >which are still correctly named, the procedure should apply only on files >which are exactly 44 characters long as the wrongly named files in this >example are.
I suggest a slightly different solution:
For each filename, check, whether it starts with a sequence of at
least two identical patterns each consisting of 8 digits, one
underscore, 6 digits and one underscore. If that's the case,
strip all but one of these patterns from the start of the
filename.
(
for f in *.jpg
do
if sequence="$(
expr ' '"$f" : \
' \(\([[:digit:]]\{8,\}_[[:digit:]]\{6,\}_\)\2\{1,\}\)'
)"
then
date_info="${sequence%${sequence#????????_??????_}}" &&
renamed_f="${date_info}${f#${sequence}}" &&
mv -i -- "$f" "$renamed_f"
fi
done
)
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)