how to deal the file with unix timestamp as name?

Started by qrtu23zzgb, December 22, 2018, 12:15:26 PM

Previous topic - Next topic

qrtu23zzgb

got lots of images named as unix timestamp (in ms), like: 1365500670141.jpg

I would like to change them as: 20130409_094430_141.jpg

is there any command exiftool can handle this task without assort with other tools?

Hayo Baan

If you're on a mac (which I am assuming since your time is in epoch starting at 1/1/2001 instead of 1/1/1970 for UNIX), you can try this:

for f in FILES; do
    mv $f `date -j -u -r ${f/%???.???/}  +"%Y%m%d_%H%M%S_${f: -7:7}"`
done


Details about the date command used:

  • -j prevents setting the date
  • -u shows the time in UTC
  • -r specifies a time in seconds since epoch (${f/%???.???/} strips the file extension and the last three digits off the filename)
  • +"%Y%m%d_%H%M%S_${f: -7:7}" specifies the output format ("year month day _ hour minute second _" followed by the three ms digits and the file extension

Hope this helps,
Hayo
Hayo Baan – Photography
Web: www.hayobaan.nl

qrtu23zzgb

thanks @hayo, but Im in windows. :)

yes, linux/macos can do a more straight way to rename these files, but Im in windows, would like a short command with exiftool. thanks.

StarGeek

This is messy and there might be a better way, but it works

exiftool "-testname<${filename;m/(^\d{10})(\d+)(.*)/;my ($sec, $min, $hour, $mday, $mon, $year)=localtime($1);$_=sprintf('%0.2d%0.2d%0.2d_%0.2d%0.2d%0.2d_%0.3d',($year+1900),$mon+1,$mday,$hour,$min,$sec,$2).$3;}" FileOrDir


C:\>exiftool "-testname<${filename;m/(^\d{10})(\d+)(.*)/;my ($sec, $min, $hour, $mday, $mon, $year)=localtime($1);$_=sprintf('%0.2d%0.2d%0.2d_%0.2d%0.2d%0.2d_%0.3d',($year+1900),$mon+1,$mday,$hour,$min,$sec,$2).$3;}" "Y:\!temp\bb\1365500670141.jpg"
'Y:/!temp/bb/1365500670141.jpg' --> 'Y:/!temp/bb/20130409_024430_141.jpg'
    0 image files updated
    1 image files unchanged


I get a final time of 024430 rather than 094430, but I'm guessing that's due to time zone difference (Pacific time zone).
"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

Hayo Baan

If you need to convert the timestamp to the local time zone time, Stargeek's command is the way to do it. If you want it in UTC, change localtime to gmtime. If you need it in a specific time zone, this can be done too, but will require some additional tampering.
Hayo Baan – Photography
Web: www.hayobaan.nl

qrtu23zzgb

@StarGeek @Hayo_Baan you guys are star! thank you very much.

Phil Harvey

Here is another way using some ExifTool helper functions and the inverse "%s" date/time conversion:

exiftool "-testname<${filename;s/(\d{3})\..*//;$_=$self->InverseDateTime($_);DateFmt(qq(%Y%m%d_%H%M%S_$1))}.%e" -d %s DIR

Change "testname" to "filename" to actually do the renaming.

This works on Mac (with different quotes), but I'm not sure if the inverse date/time formatting feature will work the same on Windows.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

Quote from: Phil Harvey on December 23, 2018, 05:38:24 PMI'm not sure if the inverse date/time formatting feature will work the same on Windows.

It does work on Windows.

I knew there had to be a better way!
"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

teredactle

Oh wow, ok this is exactly what I was looking for. I am capturing some photos off a Wyze cam, the app saves them in this format:

A4DA222C277C_1591501987590.png

First part is the MAC address, the latter is the time in Epoch time I found out...
Is there a way to have a one line command that would take the above filename, and instead turn it into this:

YYYY.MM.DD.HH.MM.SS-A4DA222C277C_1591501987590.png

If you could point me in the right direction to search this, I would love it. I would like to do it to multiple files in the current folder.

Thank you!

Phil Harvey

Try this:

exiftool "-testname<${filename;my $fn=$_;/_(\d+)\./;$_=ConvertUnixTime($1/1000,1);DateFmt('%Y.%m.%d.%H.%M.%S-');$_.=$fn}" DIR

If that works, change testname to filename to actually rename the files.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

teredactle

My gosh Phil... or do I call you wizard?
;D

You have saved me a TON (metric or imperial, you choose) of manual work.

Thank you so much! bow.. bow...!

Incidentally, I did figure out/stole bits and pieces on here, on how to get the same data/time number format pulled from iPhone videos and prefix the files, where the data/time is right in the "media created" tag, however I could not find any other tool to read this info and to rename the file correctly. Also there, with the tool you designed, has allowed me to save a lot of time and avoid manually renaming the files. Great stuff!!!

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).