2

macOS sierra, iMac, swift3

I am trying to write a program that will change the creation date of a file. I have a lot of scanned photos and documents that I wish to be able to store in alphabetical or chronological order.

I have achieved this in Objective-C but am struggling in swift. The Apple API > Foundation > FileManager > setAttributes instance method should solve my problem. Instance method ----- setAttributes(:ofItemAtPath:) Declaration --- f**c setAttributes( attributes: [FileAttributeKey : Any], ofItemAtPath path: String) throws

I am unable to write this line of code so that it will compile.

var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath)
############## My code
    let fm = FileManager()
    let myPath = "/Users/jon/a-1.jpg"  //original date  30/1/2013  = d/m/y

    //Convert date string to date object
    let myDateString = "24/11/2014"  // required date
    let dateFmt = DateFormatter()
    //dateFmt.timeZone = NSTimeZone.default
    dateFmt.dateFormat = "dd/MM/yyyy"
    let myDateObject = dateFmt.date(from : myDateString)
    print("myDateString :\(myDateString) myDateObj :\(myDateObject!)")

    //declare Mutable Dictionary with key : creationDate   and my value
    var myAttributesDictionary = [FileAttributeKey : Date]()
    myAttributesDictionary[FileAttributeKey.creationDate] = myDateObject

    print("-----------------  mtAttributesDictionary")
    print(myAttributesDictionary)
    print(myAttributesDictionary[FileAttributeKey.creationDate]!)  // works
    print("### Will print myAttributesDictionary and a value for the key")   // works

    do
    {
        let fileAttributes =  try fm.attributesOfItem(atPath : myPath)  //works
        //print(fileAttributes)
        print("### Will print File Attributes")
    }
    catch let error as NSError
    {   print("ERROR  READ Attributes: \(error)") }


    do
    {
        print("### Will print my creationDateObject  :: \(myAttributesDictionary[FileAttributeKey.creationDate]!)")
 //  This line is my problem.  I cannot write a line of code that will compile
 //  var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath)
    }
    catch let error as NSError
    {   print("ERROR    ERROR  in setAttributes: \(error)") }

     }

Output ----- myDateString :24/11/2014 myDateObj :2014-11-23 13:00:00 +0000 myAttributesDictionary [__C.FileAttributeKey(_rawValue: NSFileCreationDate): 2014-11-23 13:00:00 +0000] 2014-11-23 13:00:00 +0000 ### Will print myAttributesDictionary and a value for the key ### Will print File Attributes ### Will print my creationDateObject :: 2014-11-23 13:00:00 +0000

If the line in question code is uncommented it will not compile. I have written it at least 50 times and have had many different types of compiler errors. I just don't understand what the API is requiring and I cannot find a working example. Any suggestions would be much appreciated. A solution would be even more appreciated. Thank you.

jon
  • 121
  • 5

1 Answers1

7

Did I make this complicated.

let mypath = "/path/to/file"
let myDateObject = NSDate()       // NSDate()  is todays date

let attributes = [FileAttributeKey.creationDate: myDateObject]

do {
        try FileManager.default.setAttributes(attributes, ofItemAtPath: myPath)
  } 
  catch 
  {
        print(error)
  }
jon
  • 121
  • 5