Home > Enterprise >  Mac SetFile to change date doesn't work correctely with variable
Mac SetFile to change date doesn't work correctely with variable

Time:12-20

I'm trying to change the creation date of image files to the date of the file name.

A file is named like 20221115_125630_HDR.jpg and since touch -t only changes the modified date, I'm trying out SetFile -d.

Here's my code:

for f in ./*.jpg ; do 
  t=${f##*/}
  date="'${t:4:2}/${t:6:2}/${t:0:4} ${t:9:2}:${t:11:2}:${t:13:2}'"
  SetFile -d $date $f 
done

Oddly enough, it changes the creation date to 16 December 2021 12:56 and when I plop in echo before the SetFile, it returns this:

SetFile -d '11/15/2022 09:10:26' ./20221115_091026_HDR.jpg

When I copy and paste this line, it sets the creation date to the correct date.

What gives?

CodePudding user response:

You should quote $date, not the value you assign to it.

date="${t:4:2}/${t:6:2}/${t:0:4} ${t:9:2}:${t:11:2}:${t:13:2}"
SetFile -d "$date" "$f"
  • Related