Rikard Johnels wrote:
Hello all.
Problem: I plan to rename a few thousand photos with names like imgp4711.jpg and imgp0042.pef to a date indexed version: yyyy-mm-dd_hh-mm-ss_nnnn.type where nnnn is a running number from the exif header. and type is the original filetype (.jpg, .pef, .tif etc)
I opted for bash, and started lokking around. I have checked google and i have read the man-pages, but i'll be danged if i can understand the syntax.
I first tried getting the date and time from the header by doing: head <filename> | strings | grep 200 (to get any line with 200(6) in it.) Result: 2006:01:12 08:09:52 2006:01:12 08:09:52 2006:01:12 08:09:52 (yes i get it three times)
I then tried: head <filename> |strings | grep 200 | sed 's/\:/-/g' Result: 2006-01-12 08-09-52 2006-01-12 08-09-52 2006-01-12 08-09-52
Nice! Now i have the dashes done Then i got stuck on how to replace the blank with '_' and remove the trailing two lines. AND this method doesn't give me the frame number
New approach: I extract date, time and frame with 'exiftool' which gives me
exiftool -f -Date -Time -FrameNumber filename
Date : 2006:01:12 Time : 08:09:52 Frame Number : 1845
That gave me all three. Date, time and frame I then suppressed the tags: exiftool -f -S -s -Date -Time -FrameNumber filename 2005:03:28 21:56:15 1865
added " | sed 's/\:/-/g' " to the line and got 2005-03-28 21-56-15 1865
Now i am getting somewhere..
But this is where i get stuck... I haven't found anything i found useful on google, and the manpages on sed, awk, grep and bash is way to cryptic for me.
How do i get the three lines joined into 2005-03-28_21-56-15_1865.jpg How do i preserve the file ending? And how do i error check for nonexisting data. If the image doesn't have a correct header the exiftool will output a '-' in that position. Date: - Time: - FrameNumber: -
In that case i would want the script to use current date/time and 0000 as the frame...
I have almost no programming experience at all, so any pointers fit for a non-programmer is most welcome.
The simplest would be to assign the values to variables, for example IMGDATE=`exiftool -f -S -s -Date filename` IMGTIME=`exiftool -f -S -s -Time filename` IMGFRAME=`exiftool -f -S -s -FrameNumber filename` IMGEXT=${filename##*.} and then do something like if [ "$IMGDATE" = "-" ]; then mv filename `date`-0000.$IMGEXT else mv filename $IMGDATE-$IMGTIME-$IMGFRAME.$IMGEXT fi Untested, but I think the solution will be something in this direction.