PDA

View Full Version : NSFileManager -- operating on the most recently changed file




mathcolo
Apr 9, 2009, 07:20 PM
Hello... A small question for today: I am making a little test application that takes the directory contents at a certain path and then deletes the file that was changed most recently.

This is what I have so far:

NSString *thepath = [self path]; //path is /Users/mathcolo/hello/
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *fileContents = [fm directoryContentsAtPath:thepath];
NSString *filepath;
int i;

for (i=0; i<[fileContents count]; i++) {

//Do something with the files here or something
}



kainjow
Apr 10, 2009, 11:06 AM
So what specifically is the question? How to delete a file or how to get its modified date?

mathcolo
Apr 10, 2009, 05:09 PM
So what specifically is the question? How to delete a file or how to get its modified date?

Basically I'm trying to figure out how to make it so when I click a button, the file that has been most recently changed is deleted.

gnasher729
Apr 10, 2009, 05:43 PM
Basically I'm trying to figure out how to make it so when I click a button, the file that has been most recently changed is deleted.

Well, you would have to find out when each file is modified, then you would iterate through the files and see which one was the last to be modified, and then you would delete the file.

And for the button: You would add the button to a view in interface builder, associate an action with it, and write an action method that does the deleting.

mathcolo
Apr 10, 2009, 06:13 PM
Well, you would have to find out when each file is modified, then you would iterate through the files and see which one was the last to be modified, and then you would delete the file.

And for the button: You would add the button to a view in interface builder, associate an action with it, and write an action method that does the deleting.

Ok, sounds like that would work. I guess I should be able to use attributesOfFileSystemForPath:error: and do a loop for each file.

Thanks gnasher729.