In Mac : how to Copy + Append Modified date to the file name

Dear All,

I need a scripting in MAC for this :

I have a folder that contain several files, for example Folder A with 3 files : Susan.log, Mitch.log, Bryan.log. OS : Mac

I want to copy those 3 files to folder B, but I need when copied the file name will append date time with format : yyyymmdd. With the yyyymmdd is fetched from the modified date of those 3 files.

So
File in Source Folder         Modified Date               The result of copy in Folder B
=================        ==============         =============================
Susan.log                           23-March-2010            Susan-20100323.log
Mitch.log                             18-Dec-2009                Mitch-20091218.log
Bryan.log                            7-Dec-2008                 Bryan-20081207.log

Please give me the scripting for these results : copy files from folder A to Folder B, with the files name including the modified date of the corresponding files.

I have asked the similar question for windows OS in here :
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_25582666.html#a28953704

And I already get the solutions in Windows, and I need it for Mac now. Maybe you could see that link to get some hints. thank you.
softblessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SimonCommented:
Hi, I can do this for you in about 5 hours from now, if nobody else picks it up in the meantime.

Is this something you want to run manually from the user interface (by running a script) or from a scheduled job that watches the folder and runs whenever there are changes?

There are several possible approaches, using either Applescript or terminal (UNIX shell commands) or cron job or combination. Which to use depends on how you want to run it.

Simon
0
softblessAuthor Commented:
Hi Simon,

We could wait for another 5 hours.

We want to put it as a scheduled job that watched the folder and runs whenever there are changes.

Btw, In the question I need it as Susan-20100323.log. I think it would be better to include the time also, and the date in the front. So please make it : 201003232359-Susan.log (assuming that the modified date time of Susan.log is 23:59.

Ok then, we will wait for your script.
0
SimonCommented:
Sorry, I can't do it today. If you want something quickly, you could look at rsync, (a great backup utility) and call it from a cron job. I hope to have more time tomorrow.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

SimonCommented:
Hi, sorry it's been considerably longer than 5 hours. Here's a working script. You can run it from the script editor or make it a cron job so it can be run scheduled to run every minute or every hour or whatever frequency you want. You'll need to save the code snippet in Script Editor as an application (no splashscreen, don't stay open) to be able to run it from cron.

Let me know if you have any problems running it. You could sudo to the  root account and put it on the master crontab if you want it to run  regardless of which user is currently logged in.
 
 If you don't like using a combination of tools and want a pure  Applescript app, it can be done with a stay-open version with an idle  handler, but i've run out of time for the moment.
 
 Hope it does the job for you
 
 Simon


Sample crontab
- runs the script every 1/2 hour. If you replace 0,30 with 0-59 it would run once a minute. Not advisable to schedule so frequently that it risks being called again before previous instance has completed. The fields are separated by tab characters. * means 'every'
---
# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                

  0,30 *       *       *       *       open '/Users/*YOUR USERNAME*/Applescripts/EE/BackupIfModified.app'
---
to setup a crontab if you don't have one, open the terminal and type
nano ~/.crontab
paste the lines above between the "---" into the terminal window, then type ctrl+x, then Y to confirm changes, to exit the nano text editor and come back to the shell prompt
Then type
crontab ~/.crontab
to activate your newly created or modified crontab,
then finally, type
crontab -l
to see the scheduled tasks displayed.


(* CHANGE THE PATHS shown on the next two lines to your own source and destination folders
	- or set them to 'missing value' to be prompted the first time you run the script. 
	Thereafter it will use the values you chose until you re-compile the script
	*)

property sourceFolder : alias "Macintosh HD:Users:*YOUR USERNAME*:Applescripts:EE:Source" -- missing value
property DestFolder : alias "Macintosh HD:Users:*YOUR USERNAME*:Applescripts:EE:Destination:" -- missing value
------------------------------------------------------------------------------------------------------
-- MAKE NO CHANGES BELOW THIS LINE --------------------------------------------------------------
------------------------------------------------------------------------------------------------------
on run
	if sourceFolder is missing value then
		set sourceFolder to choose folder with prompt "Set source folder"
	end if
	
	if DestFolder is missing value then
		set DestFolder to choose folder with prompt "Set destination folder"
	end if
	
	set skipCounter to 0
	set copyCounter to 0
	
	tell application "Finder"
		set filesToProc to sourceFolder's files
		
		repeat with myfile in filesToProc
			set myInfo to (get info for myfile as alias)
			set myName to myInfo's name
			if text -4 thru -1 of myName is ".log" then
				set myName to text 1 thru -5 of myName
			end if
			set myDate to my YYYYMMDDHHMMSS(myInfo's modification date)
			set backupFileName to myDate & "-" & myName & ".log"
			if ((DestFolder as string) & backupFileName) exists then
				--log "Skipping " & backupFileName & " because it already exists in the destination folder"
				set skipCounter to skipCounter + 1
			else
				set newFile to duplicate myfile to DestFolder with replacing
				set name of newFile to backupFileName
				set copyCounter to copyCounter + 1
			end if
		end repeat
	end tell
	return (copyCounter & " copied, " & skipCounter & " skipped.") as text
end run

to YYYYMMDDHHMMSS(theDate)
	set yyyy to (year of theDate) as string
	set mm to ((100 + GetMonthNumber(the month of theDate)) as string)'s text -2 thru -1
	set dd to the ((100 + (day of theDate)) as string)'s text -2 thru -1
	set hh to the ((100 + (hours of theDate)) as string)'s text -2 thru -1
	set nn to the ((100 + (minutes of theDate)) as string)'s text -2 thru -1
	set ss to the ((100 + (seconds of theDate)) as string)'s text -2 thru -1
	return yyyy & mm & dd & hh & nn & ss
end YYYYMMDDHHMMSS

to GetMonthNumber(monthOb)
	if monthOb is January then
		1
	else if monthOb is February then
		2
	else if monthOb is March then
		3
	else if monthOb is April then
		4
	else if monthOb is May then
		5
	else if monthOb is June then
		6
	else if monthOb is July then
		7
	else if monthOb is August then
		8
	else if monthOb is September then
		9
	else if monthOb is October then
		10
	else if monthOb is November then
		11
	else if monthOb is December then
		12
	else
		error "Invalid value (not a month)."
	end if
end GetMonthNumber

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
softblessAuthor Commented:
Hi Simon,

Thanks for your response. We'll try this script asap. and we'll update you the result asap.
0
SimonCommented:
HI Softbless, how did you get on with the script? Please let me know if you need any modifications or would prefer a pure Applescript version that stays open in the background and checks the folder content at a frequency you choose.

Best regards

Simon
0
softblessAuthor Commented:
Hi Simon,

I have informed your script to my partner, he's the one who execute the script in our client's place. Today, I'll ask about the script result to him, I'll let you know ASAP.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.