ExifTool Forum

ExifTool => Newbies => Topic started by: bluza26 on June 26, 2020, 11:04:34 AM

Title: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 26, 2020, 11:04:34 AM
Hello guys,

I've searched for this topic but I couldn't find any method that worked for my case and even though I'm into programming the amount of switches and options in exiftool give me a headache...

The issue that I have is that a have a lot of images saved from Facebook which are named "FB_IMG_<unix timestamp>". I'd like to parse the timestamp into the pictures as exif date and then possibly rename the files in a much better fashion (DD-MM-YYYY_HH:MM:SS). Could you please guide me on how to do that?
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: StarGeek on June 26, 2020, 11:23:07 AM
You could copy the unix time like this
exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{9,})/;$_=$1}" /path/to/files/

After that, then you could rename by date.  See the examples under Renaming Examples (https://exiftool.org/exiftool_pod.html#RENAMING-EXAMPLES) or search on the forum.
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 26, 2020, 11:34:15 AM
Tried it but got :

ParserError:
Line |
   1 |  exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{9,})/;$_=$1}" ./
     |                                                       ~
     | Use `{ instead of { in variable names.

EDIT: nvm, fixed that issue however it didn't work for my images. no changes were made :/
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: StarGeek on June 26, 2020, 12:37:37 PM
Quote from: bluza26 on June 26, 2020, 11:34:15 AM
ParserError:
Line |
   1 |  exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{9,})/;$_=$1}" ./
     |                                                       ~
     | Use `{ instead of { in variable names.

This isn't anything from exiftool.  Test on the command line before trying to call it from anything else.

QuoteEDIT: nvm, fixed that issue however it didn't work for my images. no changes were made :/

What is the exact command and exact output from the command line? It works correctly here
C:\ >exiftool -P -overwrite_original -d "%s" "-DateTimeOriginal<${filename;m/(\d{9,})/;$_=$1}" Y:\!temp\bbbb\FB_IMG_M1593184426.jpg
    1 image files updated

C:\ >exiftool -DateTimeOriginal -g1 -a -s Y:\!temp\bbbb\FB_IMG_M1593184426.jpg
---- ExifIFD ----
DateTimeOriginal                : 2020:06:26 15:13:46
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 27, 2020, 05:55:58 AM
This is what I got:

PS D:\test> exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{8,})/;$_=$1}" ./
ParserError:
Line |
   1 |  exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{8,})/;$_=$1}" ./
     |                                                       ~
     | Use `{ instead of { in variable names.

PS D:\test> exiftool -d "%s" "-DateTimeOriginal<$'{FileName;m/(\d{8,})/;$_=$1}" ./
Warning: No writable tags set from ./16-03-2020 23_53_06.jpg
Warning: No writable tags set from ./17-10-2019 08_10_39.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë37.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë48.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë38ŕ×ë08.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë39ŕ×ë48.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00.jpg
Warning: No writable tags set from ./FB_IMG_1499787692217.jpg
Warning: No writable tags set from ./FB_IMG_1499832546704.jpg
    1 directories scanned
    0 image files updated
    9 image files unchanged
PS D:\test>
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: Phil Harvey on June 27, 2020, 08:17:37 AM
This should work if you are using cmd.exe.  But if you are using PowerShell, you should use single quotes I think.

- Phil
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 28, 2020, 09:33:12 AM
Tried it again in CMD but still didnt work


D:\test>exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{8,})/;$_=$1}" ./
Warning: No writable tags set from ./16-03-2020 23_53_06.jpg
Warning: No writable tags set from ./17-10-2019 08_10_39.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë37.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë48.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë38ŕ×ë08.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë39ŕ×ë48.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00.jpg
Warning: No writable tags set from ./FB_IMG_1499787692217.jpg
Warning: No writable tags set from ./FB_IMG_1499832546704.jpg
    1 directories scanned
    0 image files updated
    9 image files unchanged

D:\test>


The thing I have noticed is that my timestamps are a lot longer than the usual ones...but they are correct as I have checked them on the online converter, dates are correct.
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: StarGeek on June 28, 2020, 11:25:16 AM
Quote from: bluza26 on June 28, 2020, 09:33:12 AM

D:\test>exiftool -d "%s" "-DateTimeOriginal<${FileName;m/(\d{8,})/;$_=$1}" ./
Warning: No writable tags set from ./16-03-2020 23_53_06.jpg
Warning: No writable tags set from ./17-10-2019 08_10_39.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë37.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë48.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë38ŕ×ë08.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë39ŕ×ë48.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00.jpg
Warning: No writable tags set from ./FB_IMG_1499787692217.jpg
Warning: No writable tags set from ./FB_IMG_1499832546704.jpg
    1 directories scanned
    0 image files updated
    9 image files unchanged

The thing I have noticed is that my timestamps are a lot longer than the usual ones...but they are correct as I have checked them on the online converter, dates are correct.

None of those are Unix time stamps.  The first seven are in Date Month Year Hour Minu Second format.  The last two have too many numbers.  1499787692217 translates to 05/18/49496 @ 8:16pm and 1499832546704 translates to 10/19/49497 @ 11:51pm.

For the unix time stamp translation, since I didn't know what time range your files had, I chose 8 or more digits, as that would cover a range from 1973 onward.  For the latter two, if the first 10 digits are the time stamp, then change {8,} into {8,10}.  Or if you know all the time stamps are after 09/09/2001, just use {10}.

For the first seven, you need a different command.
exiftool "-DateTimeOriginal<${FileName;tr/0-9//cd;s/^(\d\d)(\d\d)(\d{4})/$3$2$1/}" /path/to/files/

C:\>exiftool -P -overwrite_original "-DateTimeOriginal<${FileName;tr/0-9//cd;s/^(\d\d)(\d\d)(\d{4})/$3$2$1/}" Y:\!temp\bbbb
    1 directories scanned
    6 image files updated

C:\>exiftool -g1 -a -s -DateTimeOriginal Y:\!temp\bbbb
======== Y:/!temp/bbbb/16-03-2020 23_53_06.jpg
---- ExifIFD ----
DateTimeOriginal                : 2020:03:16 23:53:06
======== Y:/!temp/bbbb/17-10-2019 08_10_39.jpg
---- ExifIFD ----
DateTimeOriginal                : 2019:10:17 08:10:39
======== Y:/!temp/bbbb/17-10-2019 11ŕ×ë33ŕ×ë37.png
---- ExifIFD ----
DateTimeOriginal                : 2019:10:17 11:33:37
======== Y:/!temp/bbbb/17-10-2019 11ŕ×ë33ŕ×ë48.jpg
---- ExifIFD ----
DateTimeOriginal                : 2019:10:17 11:33:48
======== Y:/!temp/bbbb/17-10-2019 11ŕ×ë38ŕ×ë08.jpg
---- ExifIFD ----
DateTimeOriginal                : 2019:10:17 11:38:08
======== Y:/!temp/bbbb/29-08-2019 08_00_00..jpg
---- ExifIFD ----
DateTimeOriginal                : 2019:08:29 08:00:00
    1 directories scanned
    6 image files read


For this command, since the separating characters vary, it will strip away any non-numeric characters.  Then it will resort the first 8 characters to be in YearMonthDay order.
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 28, 2020, 12:56:29 PM
I cannot agree on the fact that these are not timestamps. Go here : https://www.epochconverter.com/ and enter the value from the 1st timestamp. It gives the correct date, but the whole time is in miliseconds which is the issue here.
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: Phil Harvey on June 28, 2020, 01:12:34 PM
Could you be more specific?  What is the value you are talking about?

QuoteWarning: No writable tags set from ./16-03-2020 23_53_06.jpg
Warning: No writable tags set from ./17-10-2019 08_10_39.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë37.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë48.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë38ŕ×ë08.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë39ŕ×ë48.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00.jpg
Warning: No writable tags set from ./FB_IMG_1499787692217.jpg
Warning: No writable tags set from ./FB_IMG_1499832546704.jpg

The first 6 files do not have an epoch timestamp.  The last 2 files do, and these may be used to set the FileModifyDate like this:

exiftool  "-filemodifydate<${filename;/(\d{10})/ and $_ = $1}" -d %s DIR

- Phil
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 28, 2020, 01:16:39 PM
I was replying to the previous comment. Let me check the method you invoked. I also want to thank you all for being extremely helpful :)

EDIT: Tried it but it didn't work


D:\test>exiftool  "-alldates<${filename;/(\d{10})/ and $_ = $1}" -d ./
No file specified

D:\test>exiftool  "-alldates<${filename;/(\d{10})/ and $_ = $1}" -d ./
No file specified

D:\test>exiftool  "-alldates<${filename;/(\d{10})/ and $_ = $1}" -d D:\test
No file specified

D:\test>exiftool  "-filemodifydate<${filename;/(\d{10})/ and $_ = $1}" -d ./
No file specified

D:\test>exiftool  "-filemodifydate<${filename;/(\d{10})/ and $_ = $1}" ./
Warning: No writable tags set from ./16-03-2020 23_53_06.jpg
Warning: No writable tags set from ./17-10-2019 08_10_39.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë37.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë33ŕ×ë48.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë38ŕ×ë08.jpg
Warning: No writable tags set from ./17-10-2019 11ŕ×ë39ŕ×ë48.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00.jpg
Warning: No writable tags set from ./29-08-2019 08_00_00_wynik.heic
Warning: No writable tags set from ./29-08-2019 08_00_00_wynik_1.heic
Warning: No writable tags set from ./FB_IMG_1499787692217.jpg
Warning: No writable tags set from ./FB_IMG_1499832546704.jpg
    1 directories scanned
    0 image files updated
   11 image files unchanged
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: Phil Harvey on June 28, 2020, 01:27:25 PM
The devil is in the details.  You didn't get the -d %s correct in any of your commands.
Title: Re: Parsing UNIX timestamp from filename as exif date
Post by: bluza26 on June 28, 2020, 01:47:49 PM
Ahhhh yess.....when I tried it with -d it didn't work but I also forgot about %s .....thank you so much!