Find and replace part of folder name and append yesterday date

Find and replace part of folder name and append yesterday date

Hello

I am trying to run a script that will replace a part of folder name and replace it with yesterdays date.

So far I had figured out just how to generate yesterday date but the rename part is tricky.

Example:

Current folder names:

0_John
0_Nancy
0_Bob

End result with added date :

04-19-2015 John
04-19-2015 Nancy
04-19-2015 Bob

It would be great if a window would prompt which folders should be renamed.

Here is what I got so far:

set the_date to (current date) - (24 * 60 * 60)
set date_name to (date string of the_date)

set the_day to day of the_date as integer as string
if (count characters in the_day) = 1 then set the_day to “0” & the_day
set the_month to month of the_date as integer as string
if (count characters in the_month) = 1 then set the_month to “0” & the_month
set the_year to year of the_date as string
set the_year to characters 1 thru 4 of the_year as string
set date_name to the_month & “-” & the_day & “-” & the_year

Thanks in advance

You may try with :

(*
set the_date to (current date) - (24 * 60 * 60)

set the_day to day of the_date as integer as string
if (count characters in the_day) = 1 then set the_day to "0" & the_day
set the_month to month of the_date as integer as string
if (count characters in the_month) = 1 then set the_month to "0" & the_month
set the_year to year of the_date as string
set the_year to characters 1 thru 4 of the_year as string
set date_name to the_month & "-" & the_day & "-" & the_year
*)
# I would use the ISO format build with
(*
set the_date to (current date) - (1 * days)
tell the_date to set date_name to (its year as text) & "-" & text -2 thru -1 of ((100 + (its month)) as text) & "-" & text -2 thru -1 of ((100 + (its day)) as text)
*)
tell ((current date) - (1 * days)) to set date_name to (its year as text) & "-" & text -2 thru -1 of ((100 + (its month)) as text) & "-" & text -2 thru -1 of ((100 + (its day)) as text)

set theFolders to choose folder with multiple selections allowed
tell application "System Events"
	repeat with aFolder in theFolders
		tell disk item (aFolder as text)
			set its name to date_name & space & item -1 of my decoupe(its name, "_")
		end tell
	end repeat
end tell

As you may see, I replaced the date dd/mm/yyyy by the ISO format yyyy-mm-dd which is better to sort items.

Yvan KOENIG (VALLAURIS, France) lundi 20 avril 2015 22:33:20

Hi,

assuming that all folder names to be replaced start with “0_” try this


set yesterday to do shell script "date -r $((`date +%s` - 86400)) '+%m-%d-%Y'"
set baseFolder to choose folder
tell application "Finder"
	repeat with aFolder in (get folders of baseFolder)
		set folderName to name of aFolder
		if folderName starts with "0_" then
			set name of contents of aFolder to yesterday & space & text 3 thru -1 of folderName
		end if
	end repeat
end tell

Note: of course the simple subtraction of 86400 seconds could fail under certain circumstances like change of daylight saving time

Hi Stefan.

That’s nice. :slight_smile:

However, there’s a slightly more straightforward way:


set yesterday to do shell script "date -v-1d '+%m-%d-%Y'"

Using the Satimage.osax AppleScript Extension extensively. :cool:


tell application "Finder" to set myFolder to insertion location as alias # ¢¢ Front Window ¢¢
set AppleScript's text item delimiters to linefeed
set folderList to find text "^.+:$" in ((glob "0_*" from myFolder as alias) as text) with regexp, all occurrences and string result
set newNameList to change "^.+:0_([^:]+):$" into (strftime ((get current date) - (24 * hours)) into "%m-%d-%Y") & " \\1" in folderList with regexp
tell application "Finder"
	repeat with i from 1 to length of folderList
		set name of alias (item i of folderList) to item i of newNameList
	end repeat
end tell

Do shell script seems fine but in practice it’s slow. On my iMac running 10.10.3

set beg to current date
repeat 10000 times
	set yesterday to do shell script "date -v-1d '+%m-%d-%Y'"
	
end repeat
tell application "SystemUIServer" to display dialog "done in " & (current date) - beg & " seconds"

returns : “done in 128 seconds”

set beg to current date
repeat 10000 times
	tell ((current date) - (1 * days)) to set yesterday to (its year as text) & "-" & text -2 thru -1 of ((100 + (its month)) as text) & "-" & text -2 thru -1 of ((100 + (its day)) as text)
	
end repeat
tell application "SystemUIServer" to display dialog "done in " & (current date) - beg & " seconds"

returns : “done in 5 seconds”

I’m not defending my code. It’s a Nigel Garvey’s one :confused:

Yvan KOENIG (VALLAURIS, France) mardi 21 avril 2015 12:14:29

Hi Yvan.

Thanks for championing my vanilla method, although “in practice”, the date text would only have to be obtained once per run, not 10000 times. :wink:

In fact, I think the vanilla code you posted must be someone else’s adaptation of mine. I’d write it as follows, which is even faster. :slight_smile: As posted below, it produces an mm-dd-yyyy date text as specified by the OP:

set beg to current date
repeat 10000 times
	tell ((current date) - (1 * days)) to tell ((its year) * 10000 + (its month) * 100 + (its day)) as text to set yesterday to text 5 thru 6 & "-" & text 7 thru 8 & "-" & text 1 thru 4
	
end repeat
tell application "SystemUIServer" to display dialog "done in " & (current date) - beg & " seconds"

I know, but in this case it’s “in practice” a single call, so it doesn’t really matter

By the way in ASOC


set dateFormatter to current application's NSDateFormatter's alloc()'s init()
dateFormatter's dateFormat = "MM-dd-yyyy"
set calendar to current application's NSCalendar's currentCalendar()
repeat 10000 times
	set yesterDay to calendar's dateByAddingUnit:(current application's NSCalendarUnitDay) value:-1 toDate:(current application's NSDate's |date|) options:0
end repeat

it takes 1,2 seconds, in Objective-C 23 milliseconds

OK, Stefan. I give up. :wink: What’s the correct version of this script?

version who?

I assembled it quickly in Xcode

So, ZeTreze. If you’re confused:

Stefan’s script in post #3 asks you to choose a folder and renames any subfolders of that folder in the manner specified in your query if their names begin with “0_”. This is probably your best bet.

With Yvan’s interpretation in post #2, you have to choose specifically the one or more folders you actually want to rename. It has a few issues, though:

  1. The handler used to strip the “_” and preceding character(s) from the names is missing from the script, so the script will just error.
  2. If the handler weren’t missing, the folders would be renamed whether or not they were named in the prescribed manner, so it would be up to you to select them carefully.
  3. The short-date format used is yyyy-mm-dd, which isn’t what you specified, but can be a better idea in many situations. Or it can be a better idea in many situations, but isn’t what you specified. You can look at it either way. :wink:

Chris’s script in post #5 assumes that the folders you want to rename are all in the Finder’s currently frontmost container window. Like Stefan’s script, it renames only folders whose names begin with “0_”, but it needs a third-party OSAX called Satimage in order to work. You’d have to download and install this before compiling and running the script.

The rest of this thread is just us enthusiasts discussing the merits of different ways of producing the date string. The test scripts show that if this had to be done ten thousand times per run instead of just once, the “vanilla” AppleScript method would be very much faster than the shell script method. My own tests at home suggest that the Satimage ‘strftime’ method would be even faster still. Stefan’s script in post #8 is utter nonsense in an AppleScript/ASOC context, doesn’t set yesterDay to the required short-date string, and doesn’t return how long it takes to do whatever it does do ten thousand times. But otherwise it appears to be very fast too. :wink: