Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Add day of week to date in file name?

I have a number of files (several hundred) that are prefixed with a date in the format of YYYY-MM-DD. I'd like to add to this date value the day of the week for that date. For example:


2015-02-09 Some file. txt


...will become...


2015-02-09 Monday Some file.txt


Is this possible via Applescript? If so, how? If not, any other recommendations on how to accomplish this rename?

Posted on Feb 9, 2015 6:49 AM

Reply
2 replies

Feb 9, 2015 7:57 AM in response to John Catalano

I DO NOT have a complete solution. But here is the guts that will give you the day of the week for the file name given the date:

#!/usr/bin/env bash


file="2015-02-09 Some file.txt"


when="${file%%\ *}"

day=$(date -j -f "%Y-%m-%d" "${when}" +%A)

new_name="${when} $day ${file#*\ }"


echo mv "${file}" "${new_name}" # remove the echo when the code is debugged

You could craft something in Automator using the "Run Shell Script" and create a drag and drop app that runs a shell code using parts of the above (you would have to fetch the file(s) to rename from the Automator input, and implement looping if more than 1 file. Actually not too difficult.


In Automator. Select New Application.

Drag "Run Shell Script" to the right work space window

Change "Pass Input" to "As Arguments"


Change the template from

for f in "$@"

do

echo "$f"

done

to something like:

for f in "$@"

do

file="${f}"


when="${file%%\ *}"

day=$(date -j -f "%Y-%m-%d" "${when}" +%A)

new_name="${when} $day ${file#*\ }"


echo mv "${file}" "${new_name}" # remove the echo when the code is debugged

done

And remove the 'echo' from in front of the 'mv' when you are happy with the code.


NOTE: if the file format is NOT exactly "YYYY-MM-DD<SPACE>other.stuff", the script will fail, and it may fail in a bad way. so do be careful with automation.

Feb 9, 2015 8:00 AM in response to John Catalano

Here:


tell application "Finder"

repeat with this_item in (get items of window 1)

set the_date to items 1 thru 10 of (get name of this_item) as string

tell me to set the_weekday to weekday of date the_date

set name of this_item to the_date & " " & the_weekday & items 11 thru -1 of (get name of this_item)

end repeat

end tell


(121971)

Add day of week to date in file name?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.