?
Solved

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

Posted on 2010-03-29
7
Medium Priority
?
700 Views
Last Modified: 2012-05-09
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.
0
Comment
Question by:softbless
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 28983643
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
 

Author Comment

by:softbless
ID: 29017440
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
 
LVL 18

Expert Comment

by:Simon
ID: 29020669
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 18

Accepted Solution

by:
Simon earned 2000 total points
ID: 29147929
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
 

Author Comment

by:softbless
ID: 29153350
Hi Simon,

Thanks for your response. We'll try this script asap. and we'll update you the result asap.
0
 
LVL 18

Expert Comment

by:Simon
ID: 30497413
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
 

Author Comment

by:softbless
ID: 30562540
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
If you have a smartphone, I'm sure you've noticed that web applications are changing. Yet, are you aware of how these changes are going to impact your organization or company?
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses
Course of the Month5 days, 7 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question