Ios: How to Find the Creation Date of a File

iOS: How do you find the creation date of a file?

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.

Retrieve file creation or modification date

Try this. I had same problem and solved with something like next:

NSURL *fileUrl = [NSURL fileURLWithPath:myFilePath];
NSDate *fileDate;
[fileUrl getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error];
if (!error)
{
//here you should be able to read valid date from fileDate variable
}

hope it helped ;)

How to get name and creation date of all files in a folder in ios?

first get list of all files from your directory & then loop until all file completes

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
for(int i=0;i<[dirContents count];i++)
{
NSDictionary* attrs = [fm attributesOfItemAtPath:[dirContents objectAtIndex:i] error:nil];

if (attrs != nil) {
NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
NSLog(@"Date Created: %@", [date description]);

}
else {
NSLog(@"Not found");
}
}

How do you get the creation date of files in a directory in Objective c

This should help:

for (NSString file in array) {
NSLog (@"%@", file);
NSString *path = [filePath stringByAppendingPathComponent: file];
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate
NSLog(@"%@",result);
}

How can I get the file creation date using URL resourceValues method in Swift 3?

You can extend URL as follow:

extension URL {
/// The time at which the resource was created.
/// This key corresponds to an Date value, or nil if the volume doesn't support creation dates.
/// A resource’s creationDateKey value should be less than or equal to the resource’s contentModificationDateKey and contentAccessDateKey values. Otherwise, the file system may change the creationDateKey to the lesser of those values.
var creation: Date? {
get {
return (try? resourceValues(forKeys: [.creationDateKey]))?.creationDate
}
set {
var resourceValues = URLResourceValues()
resourceValues.creationDate = newValue
try? setResourceValues(resourceValues)
}
}
/// The time at which the resource was most recently modified.
/// This key corresponds to an Date value, or nil if the volume doesn't support modification dates.
var contentModification: Date? {
get {
return (try? resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
}
set {
var resourceValues = URLResourceValues()
resourceValues.contentModificationDate = newValue
try? setResourceValues(resourceValues)
}
}
/// The time at which the resource was most recently accessed.
/// This key corresponds to an Date value, or nil if the volume doesn't support access dates.
/// When you set the contentAccessDateKey for a resource, also set contentModificationDateKey in the same call to the setResourceValues(_:) method. Otherwise, the file system may set the contentAccessDateKey value to the current contentModificationDateKey value.
var contentAccess: Date? {
get {
return (try? resourceValues(forKeys: [.contentAccessDateKey]))?.contentAccessDate
}
// Beginning in macOS 10.13, iOS 11, watchOS 4, tvOS 11, and later, contentAccessDateKey is read-write. Attempts to set a value for this file resource property on earlier systems are ignored.
set {
var resourceValues = URLResourceValues()
resourceValues.contentAccessDate = newValue
try? setResourceValues(resourceValues)
}
}
}

usage:

print(yourURL.creationDate)

How to get created date of file from document directory in iOS?

NSError* error = nil;
NSDictionary *file_info = [[NSFileManager defaultManager] attributesOfItemAtPath: your_path error:&error];
NSDate *modified = file_info[NSFileModificationDate];
NSDate *created = file_info[NSFileCreationDate];

Can I change a file's modification or creation date on iOS?

You can change the modification date by running touch -t <date> <file>, where <date> is the new date and <file> is the file you want to modify. See the man page for touch for the date format, but it's basically like YYYYMMDDhhmm.SS (year, month, day, hour, minute, second).

Example:

$ touch -t 197501020304.05 foo
$ ls -lT foo
-rw-r--r-- 1 userid staff 0 Jan 2 03:04:05 1975 foo

From the documentation, it looks like you can also set the modification date using NSFileManager's setAttributes(_:ofItemAtPath:) method, which lets you set attributes for a given file, including creationDate and modificationDate. However, note that:

As in the POSIX standard, the app either must own the file or directory or must be running as superuser for attribute changes to take effect.

Swift 2 iOS - get file list sorted by creation date - more concise solution?

A possible solution:

if let urlArray = try? NSFileManager.defaultManager().contentsOfDirectoryAtURL(directory,
includingPropertiesForKeys: properties, options:.SkipsHiddenFiles) {

return urlArray.map { url -> (String, NSTimeInterval) in
var lastModified : AnyObject?
_ = try? url.getResourceValue(&lastModified, forKey: NSURLContentModificationDateKey)
return (url.lastPathComponent!, lastModified?.timeIntervalSinceReferenceDate ?? 0)
}
.sort({ $0.1 > $1.1 }) // sort descending modification dates
.map { $0.0 } // extract file names

} else {
return nil
}

The array of URLs is mapped to an array of (lastPathComponent, lastModificationDate) tuples first, then sorted according to the
last modification date, and finally the path name extracted.

The attributesDictionary can be avoided by using
getResourceValue(_ : forKey) to retrieve only the last modification date.

Update for Swift 3:

let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
if let urlArray = try? FileManager.default.contentsOfDirectory(at: directory,
includingPropertiesForKeys: [.contentModificationDateKey],
options:.skipsHiddenFiles) {

return urlArray.map { url in
(url.lastPathComponent, (try? url.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate ?? Date.distantPast)
}
.sorted(by: { $0.1 > $1.1 }) // sort descending modification dates
.map { $0.0 } // extract file names

} else {
return nil
}


Related Topics



Leave a reply



Submit