Question on how to write renaming script
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. TIA -- /Rikard ----------------------------------------------------------------------------- email : rikard.j@rikjoh.com web : http://www.rikjoh.com mob : +46 (0)736 19 76 25 ------------------------ Public PGP fingerprint ---------------------------- < 15 28 DF 78 67 98 B2 16 1F D3 FD C5 59 D4 B6 78 46 1C EE 56 >
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.
On Saturday 28 January 2006 21:49, Anders Johansson wrote:
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)
...Lines cut...
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.
Hi Anders. This implies running the exiftool three times to get the variables filled, right? As exiftool is quite slow in execution i would have hoped to refrain from running it more than once in my script. But i will surely look into this approach and do some experimenting. -- /Rikard ----------------------------------------------------------------------------- email : rikard.j@rikjoh.com web : http://www.rikjoh.com mob : +46 (0)736 19 76 25 ------------------------ Public PGP fingerprint ---------------------------- < 15 28 DF 78 67 98 B2 16 1F D3 FD C5 59 D4 B6 78 46 1C EE 56 >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The Saturday 2006-01-28 at 22:05 +0100, Rikard Johnels wrote:
This implies running the exiftool three times to get the variables filled, right? As exiftool is quite slow in execution i would have hoped to refrain from running it more than once in my script. But i will surely look into this approach and do some experimenting.
There are more tools: cer@nimrodel:~/Mail> apropos exif exif (1) - shows EXIF information in JPEG files exiftran (1) - transform digital camera jpeg images jhead (1) - Digicam JPEG Exif header manipulation tool exif (n) - Tcl EXIF extracts and parses EXIF fields from digital images - -- Cheers, Carlos Robinson -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Made with pgp4pine 1.76 iD8DBQFD2/a1tTMYHG2NR9URAihkAJ0bjaJdAxpfzl4K5t1BSNeZIyy9xwCcCF4a dmRv4P0BgoSInqlsAI+TqJg= =kghw -----END PGP SIGNATURE-----
Rikard Johnels wrote:
This implies running the exiftool three times to get the variables filled, right? As exiftool is quite slow in execution i would have hoped to refrain from running it more than once in my script. But i will surely look into this approach and do some experimenting.
Well, I'm not very good at bash scripting, so I'm sure this can be vastly improved upon, but I got it working using something like exiftool -f -S -s -Date -Time -FrameNumber filename|(read IMGDATE read IMGTIME read IMGFRAME if [ "$IMGDATE" = "-" ]; then and so on as in my other mail, finished up with a ), then put the whole thing inside a "for filename in *.jpg *.pef *.whatever; do" and end it with a "done"
participants (3)
-
Anders Johansson
-
Carlos E. R.
-
Rikard Johnels