I have a program, when run, creates a report in a certain location (/usr/local/tripwire/report) with the name:
The last group of numbers is the date-time. Every time this program is run it saves the report with a new date and time. What I would like to do is get the most recent report and copy it to my desktop. Is this possible?
The easiest way would be to use the command line…
do shell script "cp /usr/local/tripwire/report/20031004-011718.twr /$HOME/desktop/20031004-011718.twr"
Since there’s no easy way to tell the name of the file you want to copy (it’s always changing, and can’t necessarily be based on the current date), the easiest way is to use the shell:
-- first get the name of the latest file in the directory
set filename to do shell script "ls -tr /usr/local/tripwire/report | tail -1"
-- now move it, either via the finder, or via another shell script:
do shell script "cp /usr/local/tripwire/report/" & filename & " $HOME/desktop"
The switches passed to ls are -t which sorts files by time, and -r which reverses the order, essentially giving you a reverse-cron listing with the oldest files at the top and the newest at the bottom. A simple tail then grabs the last item.
From a “pure” AppleScript point of view, you might be able to get away with:
tell application "Finder"
move (last file of folder "Macintosh HD:usr:local) to desktop
but this relies of the Finder maintaining ‘last file’ as the most recently-modified file in the directory. While my (limited) testing here seems to hold out, I’m not sure I’d trust it.
I want to copy (ONLY) the most recent file since all files have a different date and time name. I like your syntax but it only allows me to copy a file with that specific name. I need some command that will search for the most recent file (in this directory) no matter what the name, and copy it to the desktop. Hope this helps.
Greg and Camelot,
I am always amazed at the programming ability of you guys. Not only you guys but the other scripters as well who post answers to this forum. I thank you all for the help.