Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve file creation or modification date

I'm using this piece of code to try to retrieve the last modified date of a file:

NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error];

        if (attributes != nil) {
            NSDate *date = (NSDate*)[attributes objectForKey: NSFileModificationDate];
            NSLog(@"Date modiifed: %@", [date description]);
        }
        else {
            NSLog(@"Not found");
        }

This works well for files in the main bundle but not if the file is located in a subdirectory of the app's document folder, with myFilePath like this:

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/The App ID Number/Documents/mySubdirectory/My Saved File

It keeps returning "not found".

I know the file is there, as I can view it with finder. I also tried removing the spaces in the file name but this had no effect.

The error log says no such file or directory, so it looks like something must've gone wrong when I tried to copy the file to the document directory.

Weird thing is, iterating through the document sub directory with contentsOfDirectoryAtPath shows the file as being present.

I've tried hard-coding the path and retrieving it programmatically, with:

*myFolder = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
*myFilePath = [myFolder stringByAppendingPathComponent:theFileName];

Can anyone see where I'm going wrong?

like image 644
Robert Avatar asked Nov 21 '12 16:11

Robert


People also ask

How can I get the date modified of a file?

Windows file properties You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

How do I find the creation date of a file?

GetCreationTime(String) Returns the creation date and time of the specified file or directory.

How do I find file creation and modification date time in Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.


5 Answers

Swift 3 solution:

func fileModificationDate(url: URL) -> Date? {
    do {
        let attr = try FileManager.default.attributesOfItem(atPath: url.path)
        return attr[FileAttributeKey.modificationDate] as? Date
    } catch {
        return nil
    }
}
like image 72
coldfire Avatar answered Oct 20 '22 07:10

coldfire


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 ;)

like image 33
zvjerka24 Avatar answered Oct 20 '22 06:10

zvjerka24


Here is a Swift like solution of @zvjerka24 answer:

func lastModified(path: String) -> NSDate? {
    let fileUrl = NSURL(fileURLWithPath: path)
    var modified: AnyObject?
    do {
        try fileUrl.getResourceValue(&modified, forKey: NSURLContentModificationDateKey)
        return modified as? NSDate
    } catch let error as NSError {
        print("\(#function) Error: \(error)")
        return nil
    }
}
like image 6
FBente Avatar answered Oct 20 '22 06:10

FBente


If you get the error:

"CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme"

You can try to solve this by appending "file:///" to your NSString file path before converting it to NSURL, it worked in my case.

like image 3
Tonnie Avatar answered Oct 20 '22 07:10

Tonnie


Can also do:

NSURL* file = ...
NSError* error;`
NSDate *creationDate = [[NSFileManager defaultManager] attributesOfItemAtPath:file.path error:&error].fileCreationDate;
like image 3
Alexandre G Avatar answered Oct 20 '22 07:10

Alexandre G