Reformatting YYYYMMDD Creation Date in Automator Shell Script to YYMMDD Creation Date Instead

30 views Asked by At

I just want to change my automator quick action from my current script of YYYYMMDD to YYMMDD. Seems like it should be simple enough but I've been trying to figure this out for days now && I'm overwhelmed.

Hi, this is what I have:

for f in "$@"
do
filedate=$(date -r $(stat -f %B $f) +%Y%m%d);
filename=$f:t
filepath=$f:h
mv $filepath/{"$filename","$filedate $filename"}
done

I have searched everywhere on how to adjust this && I can't seem to figure it out. I know, I know that YYYYMMDD is preferred but I want it in the YYMMDD format.

Also, I tried doing this:

for f in "$@"
do
filedate=$(date -r $(stat -f %B $f) +%Y%m%d_);
filename=$f:t
filepath=$f:h
mv $filepath/{"$filename","$filedate $filename"}
done

At the end of the date I put an underscore as I want all my dates to follow with that. But when I run it, it comes back with a space after. So YYYYMMDD_ blahblah.png for examples. How do I get rid of this space since it's not even in the script? Also, recommendations on where I can learn more about automator && scripting would be great. I know there's a ton of stuff out there but if there are any particular recommendations that would awesome. Thanks.

1

There are 1 answers

2
Keith Thompson On

In the format string used with the date command, %Y expands to the 4-digit year (currently "2024"), and %y expands to a 2-digit year (currently "24"). Just change %Y to %y.

(I'll take your word for it that you have a good reason to do this.)

As for the space, it's in your mv command:

mv $filepath/{"$filename","$filedate $filename"}
                                    ^HERE

You want, I presume, to concatenate $filedate (which has a trailing underscore) and $filename with no space.

mv $filepath/{"$filename","$filedate$filename"}

Or you could set $filedate to a value without the underscore, and add the underscore in the mv command:

mv $filepath/{"$filename","${filedate}_$filename"}

I personally think that's cleaner. Note that you need to add curly braces because _ could be part of an identifier.