Rename folder with current date

I have a script that copies a “template” folder to the current folder. Would like to add to that script a routine that automatically renames the copied template folder as “yymm mmm yyyy” format.

Example rename “form template folder” to “2001 Jan 2020”.

Here’s the script that copies the template folder:

set template to "Main:Users:username:Project Forms:form template folder"

tell application "Finder"
	set theTarget to (target of first Finder window) as alias
	duplicate folder template to theTarget
end tell

You may try :

set template to (path to home folder as text) & "Project Forms:form template folder"
tell (current date) to set newName to (((its year) div 100) * 100 + (its day) as text) & space & text 1 thru 3 of (its month as text) & space & its year

tell application "Finder"
	set theTarget to (target of first Finder window) as alias
	set newFolder to duplicate folder template to theTarget as alias
	set name of newFolder to newName
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 16:41:43

According to peavine and StefanK comments, it appears that the instruction building the newName must be:

tell (current date) to set newName to (((its year) mod 100) * 100 + (its month) as text) & space & text 1 thru 3 of (its month as text) & space & its year

Yvan. Does the following break next year (2021) and does it return day rather than month?

tell (current date) to set newName to (((its year) div 100) * 100 + (its day) as text)

Alternatively you can create the date string with the shell

set dateString to do shell script "date '+%y%m %b %Y'"

According to the template yymm means last two digits of the year and two digit numeric month representation

I’m not sure that I understand your question.
Maybe, it would have been more legible to keep the original code:

tell (current date)
	set newName to (((its year) div 100) * 100 + (its day) as text) --> "2022"
	set newName to newName & space & text 1 thru 3 of (its month as text) --> "2022 Jan"
	set newName to newName & space & its year --> "2022 Jan 2020"
end tell

I thought that in the OP’s question, “2001” was supposed to be built from the leading two digits of the year followed by the day number using two digits.
But maybe I misunderstood what it really was supposed to be.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 18:51:45

According to your message and StefanK’s one I edited my original message

Hello StefanK

Here running your code I got:
error “sh: -c: line 0: unexpected EOF while looking for matching `‘’
sh: -c: line 1: syntax error: unexpected end of file” number 2

I had to use :

set dateString to do shell script "date +20%m %d %Y"

replacing the standard spaces by NO-BREAK ones.

On my side I don’t use the shell to format dates because for years, in my library I may read :

(do shell script “date -u +%Y-%m-%d” & character id 160 & “%H%M%S”) to get UTC date-time

the no-shell version is 22 times faster than the shell one

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 18:56:43

My bad, there is a single quote missing before the last double quote. I edited the post

Thank you StefanK

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 19:39:27

Yvan. Your script in post 2 returns the following for newName:

“2022 Jan 2020”

Based on my understanding of what the OP wants, characters 1 and 2 are correct, but only because the current year is 2020, and characters 3 and 4 should be the month. Thus for today’s date:

“2001 Jan 2020”

One year from today newName will return the following:

“2022 Jan 2021”

It’s my understanding (thanks StefanK) that the OP would instead want:

“2101 Jan 2021”

It’s indeed incorrect. Please have a look at the specifier template

[format]yymm mmm yyyy[/format]

2-digit year
2-digit month
abbreviated month name
4-digit year

In 2021 it’s supposed to be 2101 Jan 2021

Thanks StefanK.I will correct my post 9.

(Yesterday 01:48:19 pm) I added a correction at the bottom of my message #2 but you missed it.

tell (current date) to set newName to (((its year) mod 100) * 100 + (its month) as text) & space & text 1 thru 3 of (its month as text) & space & its year

return “2001 Jan 2020”. I guess that I will never see it returning “2101 Jan 2020” :wink:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 23 janvier 2020 10:40:38

Yvan. That works great. This thread was made a bit confusing (at least for me) by the unusual date formatting desired by the OP and by all the 2’s and 0’s in the dates.

Just for fun: :slight_smile:

tell (current date)'s {year, month} to tell space & (beginning * 100 + end) & (space & end) to set newName to text 4 thru 11 & text 1 thru 5

Hello Nigel

It will not be so funny when you will run in january next year.
You will get “2101 Jan 2022” when it’s supposed to return “2001 Jan 2022” :wink:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 23 janvier 2020 16:03:19

Yvan–that doesn’t appear to be the case?

@Nigel Brilliant :cool: :cool:

@Yvan The script does exactly what’s supposed to do

I must apologize, you’re right (peavine too).

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 23 janvier 2020 17:35:24

Thank you all. This works perfectly. And I’m sorry for not being more clear on the date formatting.