I know I can use attributesOfItemAtPath to get the file modification time / date among other things ... but is there a way to set the file modification date / time?
I looked. How to set file modification time programmatically? but it does not seem to be applicable.
I request that you use http://allseeing-i.com/ASIHTTPRequest/ (as shown below) to retrieve the file ... however the timestamp is not saved the same as Last-Modified time on the server. I want to use the Last-Modified header to set the date / time of the downloaded file so that I can save the downloaded time on my system the same as on the server.
My code is:
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 lastModified line contains the time / date stamp. Is there a way to make sure the file in librarySQLGZipPath will now be set to the date / time in lastModified ? Using the current method, the librarySQLGZipPath file contains the time that was loaded, which is wrong for me.
Thanks!
Yann
EDIT LATER:
Because I wanted to nicely put on this page how to make a code for this, and still wanted to give Steven Cramer credit for an answer. Now I will edit the question with the answer that I received using the code:
whereas NSDateFromRFC1123 is the usual program found here: http://blog.mro.name/2009/08/nsdateformatter-http-header/ (with some settings) to change the Last-Modified header line to an 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 a lot to Stephen!
source share