I've got a folder being generated with today's date with another AppleScript script, and I'm now wanting to copy that folder with today's date to another hard drive. I'm very new to AppleScript and am trying to figure out how to copy a folder from one hard drive to another, given the file name changes every time the day changes.
The code I'm using to create the folder with a specific date is below.
tell application "System Events"
delay 0.5
do shell script "date +\"%m.%d.%y\""
keystroke result
delay 0.5
keystroke return
end tell
This script should address your basic request. It gets the current date and builds a source and destination path with it. It then uses the ditto
command to copy the contents of the source directory to the corresponding destination directory. If the destination does not yet exist, it will create it. This should include any sub-directories as well.
use scripting additions
-- do shell script "date +\"%m.%d.%y\""
set date8 to do shell script "date +\"%Y.%m.%d\""
set srcRoot1 to path to desktop as text
set srcRoot2 to POSIX path of srcRoot1
--> /Users/username/Desktop/2022.10.04/
set destRoot1 to path to documents folder as text
set destRoot2 to POSIX path of destRoot1
--> /Users/username/Documents/2022.10.04
do shell script "ditto -V " & quoted form of (srcRoot2 & date8 & "/") & space & quoted form of (destRoot2 & date8) & " 2>&1"
--> "ditto -V '/Users/username/Desktop/2022.10.04/' '/Users/username/Documents/2022.10.04' 2>&1"
The results above show the shell command after it is resolved. There are two optional components here. The -V
option prints a list of copied files to stderr. The closing 2>&1
sends stderr to the script result where it can be seen.