Help with a script
Posted: Fri Feb 14, 2020 2:30 pm
I have been using Hazel in a very simple way and just recently discovered how powerful and useful it is, I am definitely not educated and knowledgeable enough to write the script on my own but found on this wonderful forum something I could use. My goal is to set the creation date of the file based on the date contained in the its name, unfortunately I have been bending my file name convention to make the script I found work and have the file creation date work. Needless to say that my attempt to modify the script for my file miserably failed.
My current files structure is the follow YYYY-MM-DD_Description_Type.pdf, the below script is for a file with the follow structure, YYYY-MM-DD--Description.pdf
Can someone point me in the right direction so I can get the script change to accommodate my file name structure? Any help on this matter is highly appreciated. TIA
My current files structure is the follow YYYY-MM-DD_Description_Type.pdf, the below script is for a file with the follow structure, YYYY-MM-DD--Description.pdf
- Code: Select all
Shell /bin/bash
# Take filename, YYYY-MM-DD--New Name.pdf and
# - adjust the creation date using /usr/bin/setfile -d
# --------------------------------------------------------------------------
# -- Commented Out --
# - adjust the modification date using /usr/bin/touch
# - rename the file to New Name.pdf using /bin/mv
# --------------------------------------------------------------------------
filename_without_path=$(basename "$1")
extension="${filename_without_path##*.}"
filename_without_extension_or_path="${filename_without_path%.*}"
YYYY=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $1}')
MM=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $2}')
DD=$(echo "$filename_without_extension_or_path" | awk -F "-" '{print $3}')
NNAME=$(echo "$filename_without_extension_or_path" | awk -F "--" '{print $4}')
# Change creation date
/usr/bin/setfile -d "$MM/$DD/$YYYY" "$1"
# --------------------------------------------------
Can someone point me in the right direction so I can get the script change to accommodate my file name structure? Any help on this matter is highly appreciated. TIA