-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacdatetree.sh
More file actions
84 lines (67 loc) · 2.66 KB
/
macdatetree.sh
File metadata and controls
84 lines (67 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/zsh
# Function to extract the date from the folder name in the format "long month (%B), day of month (%d), year (%Y)"
get_date_from_folder() {
local folder_name=$1
# Regular expression pattern to match "long month (%B), day of month (%d), year (%Y)" format in the folder name
local pattern='([[:alpha:]]+ [0-9]{1,2}, [0-9]{4})'
local date_string
if [[ $folder_name =~ $pattern ]]; then
date_string="${MATCH}"
else
date_string=""
fi
echo "$date_string"
}
# Function to rename the folder with the new date format
rename_folder_with_date() {
# the current folder we are working on
local folder_name=$1
local new_date_format=$2
local export_label=$3 # New argument captured here
# Extract the parent directory's path
local parent_path=$(dirname "$folder_name")
local subfolder="${folder_name#${parent_path}}"
subfolder="${subfolder:1}"
local old_date=$(get_date_from_folder "$folder_name")
local new_subfolder="${subfolder//$old_date/}"
# Check if the last character of new_subfolder is a comma and remove it
if [[ "${new_subfolder: -1}" == "," ]]; then
new_subfolder="${new_subfolder%,}"
fi
if [[ -n "$old_date" ]]; then
local new_date=$(date -j -f "%B %d, %Y" "$old_date" +"$new_date_format")
# CHANGED: Now uses ${export_label} instead of hardcoded "iPhoto"
local new_folder_name="${parent_path}/${new_date} ${new_subfolder} ${export_label} Export"
# keeping around the following for future debugging
#echo "Old Folder: $folder_name"
#echo "Parent Path: $parent_path"
#echo "Subfolder: $subfolder"
#echo "Old Date: $old_date"
#echo "New Date: $new_date"
#echo "New Folder: $new_folder_name"
#echo " "
mv "$folder_name" "$new_folder_name"
else
echo "No Date Found in Folder: $folder_name"
echo " "
date_string=""
fi
}
# Main script logic
main() {
local new_date_format="%Y-%m-%d"
# Check for a command line argument ($1).
# If it exists, use it. If not, default to "iPhoto".
local export_label="${1:-iPhoto}"
# Get the current directory (current folder)
local current_directory="${PWD}"
# Iterate through all subdirectories in the current directory
for folder in "${current_directory}"/*(/); do
if [[ -d "$folder" ]]; then
# Call the rename function for each folder, passing the custom label
rename_folder_with_date "$folder" "$new_date_format" "$export_label"
fi
done
}
# Call the main function with all arguments ("$@") passed to the script
main "$@"