Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 文件从目录文件夹中删除_Swift_Date_Swift3 - Fatal编程技术网

Swift 文件从目录文件夹中删除

Swift 文件从目录文件夹中删除,swift,date,swift3,Swift,Date,Swift3,如何从目录中的每个文件中获取日期 let directoryContent = try fileManager.contentsOfDirectory(atPath: directoryURL.path) 这就是我从目录中获取文件的方式。我找到了一些方法: directoryContent.Contains(…) 文件中的数据比前几天旧-我如何检查它 然后, 它将给我目录中的最后一个文件 这将以字节为单位返回日期: for var i in 0..<directoryContent.co

如何从目录中的每个文件中获取日期

let directoryContent = try fileManager.contentsOfDirectory(atPath: directoryURL.path)
这就是我从目录中获取文件的方式。我找到了一些方法:

directoryContent.Contains(…)

文件中的数据比前几天旧-我如何检查它

然后,

它将给我目录中的最后一个文件

这将以字节为单位返回日期:

for var i in 0..<directoryContent.count {
                let date = directoryContent.index(after: i).description.data(using: String.Encoding.utf8)!
                print(date)
            }

对于0..中的var i,强烈建议使用
FileManager
URL
相关API以非常有效的方式获取文件属性

此代码打印创建日期早于一周前的指定目录的所有URL

let calendar = Calendar.current
let aWeekAgo = calendar.date(byAdding: .day, value: -7, to: Date())!

do {
    let directoryContent = try fileManager.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: [.creationDateKey], options: .skipsHiddenFiles)
    for url in directoryContent {
        let resources = try url.resourceValues(forKeys: [.creationDateKey])
        let creationDate = resources.creationDate!
        if creationDate < aWeekAgo {
            print(url)
            // do somthing with the found files
        }
    }
}
catch {
    print(error)
}
let calendar=calendar.current
让aWeekAgo=calendar.date(通过将:.day,值:-7添加到:date())!
做{
让directoryContent=try fileManager.contentsOfDirectory(位于:directoryURL,包括属性forkeys:[.creationDateKey],选项:.skipsHiddenFiles)
对于directoryContent中的url{
让resources=尝试url.resourceValues(forKeys:[.creationDateKey])
让creationDate=resources.creationDate!
如果creationDate
如果希望更好地控制工作流,例如URL无效,并且希望打印错误URL和相关错误,但继续使用枚举器处理其他URL,则语法非常类似:

do {
    let enumerator = fileManager.enumerator(at: directoryURL, includingPropertiesForKeys: [.creationDateKey], options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
        print("An error \(error) occurred at \(url)")
        return true
    })
    while let url = enumerator?.nextObject() as? URL {
        let resources = try url.resourceValues(forKeys: [.creationDateKey])
        let creationDate = resources.creationDate!
        if creationDate < last7Days {
            print(url)
            // do somthing with the found files
        }
    }
    
}
catch {
    print(error)
}
do{
让enumerator=fileManager.enumerator(位于:directoryURL,包括属性Forkeys:[.creationDateKey],选项:[.SkipSubDirectoryDescents,.skipsHiddenFiles],errorHandler:{(url,error)->Bool-in
打印(“错误\(错误)发生在\(url)”)
返回真值
})
而让url=enumerator?.nextObject()作为?url{
让resources=尝试url.resourceValues(forKeys:[.creationDateKey])
让creationDate=resources.creationDate!
如果创建日期<最后7天{
打印(url)
//对找到的文件执行某些操作
}
}
}
抓住{
打印(错误)
}

这正是我想要的!!非常感谢。
do {
    let enumerator = fileManager.enumerator(at: directoryURL, includingPropertiesForKeys: [.creationDateKey], options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
        print("An error \(error) occurred at \(url)")
        return true
    })
    while let url = enumerator?.nextObject() as? URL {
        let resources = try url.resourceValues(forKeys: [.creationDateKey])
        let creationDate = resources.creationDate!
        if creationDate < last7Days {
            print(url)
            // do somthing with the found files
        }
    }
    
}
catch {
    print(error)
}