• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting a file's modification date.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting a file's modification date.


  • Subject: Re: Getting a file's modification date.
  • From: Clark Mueller <email@hidden>
  • Date: Fri, 13 Sep 2002 19:15:50 -0600

On Friday, September 13, 2002, at 03:19 PM, Benjamin Salanki wrote:

> Hi!
>
> I know this must be trivial, but I just can't figure out how to get a
> file's modification date. I tried
>
> NSDictionary *fattrs = [manager
> fileAttributesAtPath:@"/private/var/tmp/console.log" > traverseLink:YES];
>
> but everytime I try to get info from the NSDictionary I get a (null)
> pointer.
>
> what am I doing wrong?
>
> ben

It sort of sounds like there isn't a modification date for the file in
question, though that is unlikely. That is, however, the kind of result
that you would get... Here's methods I use to get and set modification
date. Note that in the set method, I do two additional things because
of my implementation. I post a notification when the method is done
executing, and I also use the method "wakeUpPath", which basically
calls NSWorkspace's noteFileSystemChanged: method, to wake up any apps
interested in my having changed the modification date.

/**************************************************/
- (NSDate *)modDateForFile:(NSString *)filename{
// Returns the modification date of 'filename' as an NSDate.

NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *fileInfo;
NSDate *localModDate;

if(![self fileExists:filename]){
return nil;
}
else{
fileInfo = [fm fileAttributesAtPath:filename traverseLink:NO];
localModDate = [fileInfo objectForKey:NSFileModificationDate];

return localModDate;
}

return nil;
}

- (BOOL)setModDateForFile:(NSDate *)date forFile:(NSString *)filename{
// Sets the modification date of 'file' to 'date'.

NSFileManager *fm = [NSFileManager defaultManager];
NSMutableDictionary *localAttributes;

if(![self fileExists:filename]){
return NO;
}
else{
localAttributes = [NSMutableDictionary dictionaryWithDictionary:[fm
fileAttributesAtPath:filename traverseLink:NO]];

[localAttributes setObject:date forKey:NSFileModificationDate];

if(![fm changeFileAttributes:localAttributes atPath:filename]){
[[NSDistributedNotificationCenter
defaultCenter]postNotificationName:@"MFIGFinishedChangeNotification"
object:nil userInfo:[NSDictionary dictionaryWithObject:filename
forKey:@"Path"]];
return NO;
}
else{
[self wakeUpPath:filename];
[[NSDistributedNotificationCenter
defaultCenter]postNotificationName:@"MFIGFinishedChangeNotification"
object:nil userInfo:[NSDictionary dictionaryWithObject:filename
forKey:@"Path"]];
return YES;
}
}

return nil;
}
/**************************************************/

I hope that helps you a little...

-c
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Printing Newbie
  • Next by Date: Re: Printing Newbie
  • Previous by thread: Re: Printing Newbie
  • Next by thread: Keyboard management?
  • Index(es):
    • Date
    • Thread