Reputation: 82219
I'm trying to find the creation date (NOT modification date) of a file.
Creation date doesn't appear to be in the attributes of a file, though modified date is.
I'm using this code..
NSFileManager* fm = [NSFileManager defaultManager];
NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];
if (attrs != nil) {
return (NSDate*)[attrs objectForKey: NSFileCreationDate];
} else {
return nil;
}
This always returns nil. Typing 'po attrs' into the debugger to get the list of key/value pairs in the NSDictionary returns the following..
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = ben;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;
No creation date.. bah..
Anyone know another way of getting the creation date or does it just not exist in iOS?
Upvotes: 47
Views: 46980
Reputation: 2315
Swift 5
let url: URL = ....
let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
if let date = attributes?[.modificationDate] {
print("File Modification date is %@", date)
}
if let date = attributes?[.creationDate] {
print("File Creation date is %@", date)
}
Upvotes: 3
Reputation: 4337
In Swift 5:
let date = (try? FileManager.default.attributesOfItem(atPath: path))?[.creationDate] as? Date
Upvotes: 9
Reputation: 36610
Updated answer for Swift 4 to pull out the modified (.modifiedDate
) or creation (.creationDate
) date:
let file: URL = ...
if let attributes = try? FileManager.default.attributesOfItem(atPath: file.path) as [FileAttributeKey: Any],
let creationDate = attributes[FileAttributeKey.creationDate] as? Date {
print(creationDate)
}
Upvotes: 12
Reputation: 12363
In Swift 4, if you want to know the file created date for a specific file in DocumentsDirectory, you can use this method
func getfileCreatedDate(theFile: String) -> Date {
var theCreationDate = Date()
do{
let aFileAttributes = try FileManager.default.attributesOfItem(atPath: theFile) as [FileAttributeKey:Any]
theCreationDate = aFileAttributes[FileAttributeKey.creationDate] as! Date
} catch let theError as Error{
print("file not found \(theError)")
}
return theCreationDate
}
Upvotes: 7
Reputation: 23655
There is a special message fileCreationDate
for that in NSDictionary
. The following works for me:
Objective-C:
NSDate *date = [attrs fileCreationDate];
Swift:
let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
attrs.fileCreationDate()
Upvotes: 23
Reputation: 1897
Swift 3 version code:
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: yourPathString)
let modificationDate = fileAttributes[FileAttributeKey.modificationDate] as! Date
print("Modification date: ", modificationDate)
} catch let error {
print("Error getting file modification attribute date: \(error.localizedDescription)")
}
Upvotes: 2
Reputation: 6132
Swift 2.0 version:
do {
let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(YOURPATH)
let creationDate = fileAttributes[NSFileCreationDate] as? NSDate
let modificationDate = fileAttributes[NSFileModificationDate] as? NSDate
print("creation date of file is", creationDate)
print("modification date of file is", modificationDate)
}catch let error as NSError {
print("file not found:", error)
}
Upvotes: 9
Reputation: 2348
NSDate *creationDate = nil;
if ([fileManager fileExistsAtPath:filePath]) {
NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
creationDate = attributes[NSFileCreationDate];
}
Its here
Upvotes: 3
Reputation: 5629
This code actually returns the good creation date to me:
NSFileManager* fm = [NSFileManager defaultManager];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];
if (attrs != nil) {
NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
NSLog(@"Date Created: %@", [date description]);
}
else {
NSLog(@"Not found");
}
Are you creating the file inside the App? Maybe that's where the problem is.
Upvotes: 73
Reputation: 19071
Can you use fstat64
to get the st_birthtimespec
member of the returned struct? You'll need to create a C file handle for the file and convert the timespec
value into an NSDate
, but that's better than nothing.
Upvotes: 1