MacOS 和快速更改/设置文件创建日期

jon*_*jon 2 attributes creation swift3

macOS 塞拉利昂、iMac、swift3

我正在尝试编写一个程序来更改文件的创建日期。我有很多扫描的照片和文档,我希望能够按字母或时间顺序存储。

我已经在 Objective-C 中实现了这一点,但在 swift 中却很挣扎。Apple API > Foundation > FileManager > setAttributes 实例方法应该可以解决我的问题。实例方法 ----- setAttributes( :ofItemAtPath:) 声明 --- f**c setAttributes( attribute: [FileAttributeKey : Any], ofItemAtPath path: String) throws

我无法编写这行代码以便它能够编译。

var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath)
Run Code Online (Sandbox Code Playgroud) ############## 我的代码
    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)") }

     }
Run Code Online (Sandbox Code Playgroud)

输出 ----- 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 ### 将打印 myAttributesDictionary 和键的值 ### 将打印文件属性 ### 将打印我的creationDateObject :: 2014-11-23 13:00: 00+0000

如果有问题的代码行未注释,它将无法编译。我已经写了至少 50 次,并且遇到过许多不同类型的编译器错误。我只是不明白 API 需要什么,而且找不到有效的示例。任何建议将不胜感激。如果有解决方案,我们将更加感激。谢谢。

jon*_*jon 7

我把这件事搞复杂了吗?

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)
  }
Run Code Online (Sandbox Code Playgroud)