Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Changing File Names and Created by date

I need to change the file name and created by date of loads of files AND change the created by date at the same time.


A long tine ago a nice online person wrote a little applescript app for me to drop files onto and that would change the created by date according to what was in the file name. The file names always start are in this format dd-mm-yy filename.extension


Eg 03-10-14 Applescript.pdf

I'd like to change the date format in the file names to yy-mm-dd Filename.extension - for sensible file naming( ok...lesson now learnt :-) I was young and foolish back then)


So 03-10-14 Applescript.pdf would become 14-10-03 Applescript.pdf and the 'created by' date changed to that date too.


Can anyone help me please?


This is the AS that was done before for me before if it help at all. They made it into a little app that sits on my dock...I just drag files onto it and they are changed automatically.


on open of theFiles
    -- This is executed when files are dropped on the script
    repeat with i in theFiles
        if folder of (info for i) then
            processAFolder(i)
        else
            processAFile(i)
        end if
    end repeat
end open

on processAFile(theFile)
    set theName to name of (info for theFile)
    try
        set dd to characters 1 through 2 of theName as text as integer
        if dd < 10 then set dd to "0" & dd
        set mm to characters 4 through 5 of theName as text as integer
        if mm < 10 then set mm to "0" & mm
        set yy to characters 7 through 8 of theName as text as integer
        if yy < 10 then set yy to "0" & yy
        do shell script "touch -t " & yy & mm & dd & "0000 " & quoted form of POSIX path of theFile
    end try
end processAFile

on processAFolder(theFolder)
    tell application "Finder"
        set theContents to items of folder theFolder as alias list
    end tell
    repeat with i in theContents
        if folder of (info for i) then
            processAFolder(i)
        else
            processAFile(i)
        end if
    end repeat
end processAFolder


That is SOOOOOO Chinese to me!!!!


Thank you all so much.


John


PS: I am happy to pay for this to be done.

MacBook Pro (15-inch Mid 2009), Mac OS X (10.7.5)

Posted on Oct 3, 2014 4:38 AM

Reply
3 replies

Oct 3, 2014 5:42 AM in response to Jacjac

Hi,


I edited your script, the file will be named as you want.


on open of theFiles -- This is executed when files are dropped on the script  
    repeat with i in theFiles
        if folder of (info for i) then
            processAFolder(i)
        else
            processAFile(i)
        end if
    end repeat
end open

on processAFile(theFile)
    set theName to name of (info for theFile)
    try
        set dd to text 1 thru 2 of theName
        set mm to text 4 thru 5 of theName
        set yy to text 7 thru 8 of theName
        do shell script "touch -t " & yy & mm & dd & "0000  " & quoted form of POSIX path of theFile
        set newName to yy & "-" & mm & "-" & dd & (text 9 thru -1 of theName)
        tell application "System Events" to set name of theFile to newName -- rename this file
    end try
end processAFile

on processAFolder(theFolder)
    tell application "Finder"
        set theContents to items of folder theFolder as alias list
    end tell
    repeat with i in theContents
        if folder of (info for i) then
            processAFolder(i)
        else
            processAFile(i)
        end if
    end repeat
end processAFolder

Oct 5, 2014 8:15 AM in response to Jacjac

Hi,


Because the 'touch' command only changes the modification and access times. It only changes the creation time if the target time is before the original creation time.



You must use cocoa frameworks to change the creation date of the files, or use the setFile command (need Xcode).





Or use this script:

it set the creation date of each file to the date from character 1 thru 8 in the name, the date format must be --> "03-10-14" (day-month-year).

This script keep the time of the creation date to not have "00:00:00" on all files

If the creation date is newer that the modification date, it change the modification date to the creation date

The script rename the file --> "14-10-03" (year-month-day)


-- this script rename files and set the creation date of each file to the date from character 1 thru 8 in the name (date format in the name : "03-10-14", day-month-year)
-- When the dialog will appear, just select a folder which contains files to change the name and the creation date

do shell script "/usr/bin/python -c 'from Foundation import NSFileManager, NSURL, NSString, NSDateFormatter, NSDirectoryEnumerationSkipsHiddenFiles, NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLCreationDateKey, NSURLNameKey; from AppKit import NSOpenPanel
def procFolder(fold):
  p=df.contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(fold, [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLCreationDateKey, NSURLNameKey], NSDirectoryEnumerationSkipsHiddenFiles, None)[0]
  for f in p:
           r=f.getResourceValue_forKey_error_(None, NSURLIsDirectoryKey, None)
           if r[0] and r[1]:
                 procFolder(f)
           else:
                  myDict, error=f.resourceValuesForKeys_error_([NSURLContentModificationDateKey, NSURLCreationDateKey, NSURLNameKey], None)
                  if error is None:
                      name=myDict.get(NSURLNameKey)
                      if name.length > 8:
                           datString=name.substringToIndex_(8)
                           timeString=datf2.stringFromDate_(myDict.get(NSURLCreationDateKey))
                           cDate=datf.dateFromString_(datString + timeString)
                           if cDate != None: ## first 8 characters in the name is a valid date
                               newName=name[6:8]  + name[2:6] + name[0:2] + name[8:]
                               dict={NSURLCreationDateKey:cDate, NSURLNameKey:newName}
                               if cDate > myDict.get(NSURLContentModificationDateKey): dict[NSURLContentModificationDateKey] = cDate
                               b, error=f.setResourceValues_error_(dict, None)
                               if not b: print f.path()

df=NSFileManager.defaultManager()
datf=NSDateFormatter.new(); datf.setDateFormat_(\"dd-MM-yyHH:mm:ss\") ;datf2=NSDateFormatter.new(); datf2.setDateFormat_(\"HH:mm:ss\") 
op=NSOpenPanel.openPanel(); op.setCanChooseDirectories_(True); op.setCanChooseFiles_(False); op.setAllowsMultipleSelection_(False)
if op.runModal() == 1: procFolder(op.URLs()[0])' "




----------

if you have already rename these files to (year-month-day), use this script

it set the creation date of each file to the date from character 1 thru 8 in the name, the date format must be --> "14-10-03" (year-month-day).

it will not rename files.


-- this script set the creation date of each file to the date from character 1 thru 8 in the name (date format in the name : "14-10-03" (year-month-day)
-- When the dialog will appear, just select a folder which contains files to change  the creation date

do shell script "/usr/bin/python -c 'from Foundation import NSFileManager, NSURL, NSString, NSDateFormatter, NSDirectoryEnumerationSkipsHiddenFiles, NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLCreationDateKey, NSURLNameKey; from AppKit import NSOpenPanel
def procFolder(fold):
  p = df.contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(fold, [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLCreationDateKey], NSDirectoryEnumerationSkipsHiddenFiles, None)[0]
  for f in p:
           r=f.getResourceValue_forKey_error_(None, NSURLIsDirectoryKey, None)
           if r[0] and r[1]:
                 procFolder(f)
           else:
                  myDict, error=f.resourceValuesForKeys_error_([NSURLContentModificationDateKey, NSURLCreationDateKey, NSURLNameKey], None)
                  if error is None:
                      name=myDict.get(NSURLNameKey)
                      if name.length > 8:
                           datString=name.substringToIndex_(8)
                           timeString=datf2.stringFromDate_(myDict.get(NSURLCreationDateKey))
                           cDate=datf.dateFromString_(datString + timeString)
                           if cDate != None: ## first 8 characters in the name is a valid date
                               dict={NSURLCreationDateKey:cDate}
                               if cDate > myDict.get(NSURLContentModificationDateKey): dict[NSURLContentModificationDateKey] = cDate
                               b, error=f.setResourceValues_error_(dict, None)
                               if not b: print f.path()

df=NSFileManager.defaultManager()
datf=NSDateFormatter.new(); datf.setDateFormat_(\"yy-MM-ddHH:mm:ss\") ;datf2=NSDateFormatter.new(); datf2.setDateFormat_(\"HH:mm:ss\") 
op=NSOpenPanel.openPanel(); op.setCanChooseDirectories_(True); op.setCanChooseFiles_(False); op.setAllowsMultipleSelection_(False)
if op.runModal() == 1: procFolder(op.URLs()[0])' "

Changing File Names and Created by date

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.