Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set attributesOfItemAtPath (such as modification date/time) on iPhone

I know I can use attributesOfItemAtPath to get modification time/date of file among other things... but is there a way to SET the modification date/time of a file?

I have looked at How to set the modification time of a file programmatically? but it does not seem to apply.

The reason I ask is I use http://allseeing-i.com/ASIHTTPRequest/ (as below) to fetch a file... however the timestamp is not kept the same as the Last-Modified time on the server. I want to use the Last-Modified header to set the date/time of the downloaded file so i can keep the downloaded time on my system the same as it is on the server.

My code follows:

ASIHTTPRequest *requestFeed = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kGZipSQLURL]];
[requestFeed setNumberOfTimesToRetryOnTimeout:2];
[requestFeed setDownloadDestinationPath:librarySQLGZipPath];
[requestFeed setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[requestFeed setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[requestFeed setSecondsToCache:60*60*24*30];
[requestFeed setDownloadCache:[ASIDownloadCache sharedCache]];
[requestFeed startSynchronous];
NSString *lastModified = [[requestFeed responseHeaders] objectForKey:@"Last-Modified"];
NSLog(@"Last Modified: %@",lastModified);

So, the string lastModified holds the time/date stamp. Is there a way to ensure that the file at librarySQLGZipPath will now be set to the date/time in lastModified? Using the current method, the file librarySQLGZipPath holds the time it was downloaded, which to me is invalid.

Thanks!

Jann

EDIT LATER:

Because I wanted to nicely put on this page how to do the code for this and still wanted to give Steven Kramer credit for answering I am now gonna edit the question with the answer I got using code:

whereas NSDateFromRFC1123 is a routine found here: http://blog.mro.name/2009/08/nsdateformatter-http-header/ (with some adjustments) to change the Last-Modified header string into a NSDate object:

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[self NSDateFromRFC1123:lastModified]  ,NSFileModificationDate, nil];
NSError *errorFile;
if ([NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:librarySQLGZipPath error: &errorFile])
{
NSLog(@"Set timestamp of file");
}
else {
NSLog(@"COULD NOT set timestamp of file");
}

Thanks so much Steven!

like image 382
Jann Avatar asked May 09 '11 15:05

Jann


1 Answers

Use -[NSFileManager setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error

like image 83
Steven Kramer Avatar answered Oct 13 '22 01:10

Steven Kramer