Batch format date in filename

I have thousands of files I have imported to DTPO 2. Unfortunately, over the past several years, I have not been entirely consistent in my naming protocols, particularly in regards to dates. My general name format is:

(Filename)(space)(date)

with the filename of variable length, and with the date consisting of one of the following formats:

Two-digit year
x-x-xx
x-xx-xx
xx-x-xx
xx-xx-xx

Four-digit year
x-x-xxxx
x-xx-xxxx
xx-x-xxxx
xx-xx-xxxx

So, basically, every configuration of one- or two-digit months, one- or two-digit days, and two- or four- digit years. The vast majority are in two-digit year format. Unfortunately, this is the result of adding files over years, and not maintaining consistency. My bad!

Since really honing in on using DT, and poring over the forum, I have found I quite like Master Bill’s date format:

YYYYMMDD

without hyphens, slashes, etc.

So I have two problems:

  1. I am not sure how to get the individual filename into AppleScript, so I can run this manipulation on that name, then add the new name back to the filename;
  2. I would like to select a group of files and run the script on each consecutively;
  3. (This is likely the hard part) I would like to create variable so that any of the above date formats will be corrected to my new format.

I had an expert help me with such a script in Finder, with excellent results:

tell application "Finder" to set theSelection to (get selection)
repeat with oneItem in theSelection
   set {name:Nm, name extension:Ex} to (info for oneItem as alias)
   set filename to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
   if Ex is missing value then set Ex to ""
   if Ex is not "" then set Ex to "." & Ex
   set {TID, text item delimiters} to {text item delimiters, space}
   tell filename to set {theName, theDate} to {(text items 1 thru -2) as text, text item -1}
   set text item delimiters to "-"
   set {mn, dy, yr} to text items of theDate
   set text item delimiters to TID
   if (count yr) = 2 then set yr to "20" & yr
   tell application "Finder" to set name of contents of oneItem to theName & space & yr & my pad(mn) & my pad(dy) & Ex
end repeat

on pad(t)
   return text -2 thru -1 of ("0" & t)
end pad

If I use this in Finder, it works perfectly. Unfortunately, I tried it within DTPro Office, and am running into some problems. And the expert does not know DT.

I have tried to open the information window, copy the name, manipulate the name using the above script, and then pasting it back. No dice.

Any help from our resident mavens?

Cheers!

Sorry to recommend a solution that involves cost, but “A Better Finder Rename” may save you hours of needless frustration:

http://www.publicspace.net/ABetterFinderRename/

I’ve certainly coded a lot of batch scripts, but I’d much rather use this nice utility.

Just realized (I’ve been working with indexed databases so much lately) that your files might be inside a DTPO database. If you want to go the script route, then something like this:

tell application "DEVONthink Pro"
	get filename of (selection as reference)
end tell

Charles

Thank you, Charles.

Unfortunately, I am working with imported files. I don’t mind at all spending for the utility, but wouldn’t I then have to re-import everything?

As to your suggestion for the script, it did help with one part: I can now get the name of the file as a result within the script, but then I get further problems (assuming I used it as you thought I should!):

tell application "DEVONthink Pro" to set theSelection to (get selection)
repeat with oneItem in theSelection
	tell application "DEVONthink Pro"
		get filename of (selection as reference)
	end tell
	
	set filename to text 1 thru ((get offset of "." in Nm) - 1) of Nm
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Ex to "." & Ex
	set {TID, text item delimiters} to {text item delimiters, space}
	tell filename to set {theName, theDate} to {(text items 1 thru -2) as text, text item -1}
	set text item delimiters to "-"
	set {mn, dy, yr} to text items of theDate
	set text item delimiters to TID
	if (count yr) = 2 then set yr to "20" & yr
	tell application "Finder" to set name of contents of oneItem to theName & space & yr & my pad(mn) & my pad(dy) & Ex
end repeat

on pad(t)
	return text -2 thru -1 of ("0" & t)
end pad
tell application "DEVONthink Pro"
	get filename of (selection as reference)
end tell

I get the following events and replies:

tell application "DEVONthink Pro"
	get selection
		--> {content id 99975 of database id 3}
	get selection
		--> {content id 99975 of database id 3}
	get filename of content id 99975 of database id 3
		--> "State Farm Cancellation 10-26-2006.pdf"
Result:
error "The variable Nm is not defined." number -2753 from "Nm"

Any suggestions?

Cheers!

Hi-

I’m leaving on a 2-week business trip tomorrow, and don’t really have time to write your script for you. It would go something like this:

tell application "DEVONthink Pro" to set theSelection to (selection as reference)
repeat with oneItem in theSelection
  set oldName to filename of oneItem
  -- Your previous script asks Finder to do some work here ("info")
  -- and you'll have to replicate that functionality in DTPO
  set newName to text 1 thru ((get offset of "." in oldName) - 1) of oldName
  if Ex is missing value then set Ex to ""
  if Ex is not "" then set Ex to "." & Ex
  set {TID, text item delimiters} to {text item delimiters, space}
  tell filename to set {theName, theDate} to {(text items 1 thru -2) as text, text item -1}
  set text item delimiters to "-"
  set {mn, dy, yr} to text items of theDate
  set text item delimiters to TID
  if (count yr) = 2 then set yr to "20" & yr
  set filename of oneItem to theName & space & yr & my pad(mn) & my pad(dy) & Ex
end repeat

on pad(t)
  return text -2 thru -1 of ("0" & t)
end pad

The biggest trick will be crafting a substitute for the “info” function. You’ll have to parse the filename string manually instead of relying on a deprecated Applescript function.

HTH, Charles

Charles-

Thank you so much for your post. I will work on it and post back with results.

I am exhausted from staying up all night trying to figure this out without luck, and your effort will make my day considerably better! :smiley:

I hope you have a great and successful business trip.

Regards,

Scott