NSFileManager -- operating on the most recently changed file

Discussion in 'Mac Programming' started by mathcolo, Apr 9, 2009.

  1. macrumors 6502a

    mathcolo

    Joined:
    Sep 14, 2008
    Location:
    Massachusetts
    #1
    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:

    Code:
    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
    }
    
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    So what specifically is the question? How to delete a file or how to get its modified date?
     
  3. thread starter macrumors 6502a

    mathcolo

    Joined:
    Sep 14, 2008
    Location:
    Massachusetts
    #3
    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.
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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.
     
  5. thread starter macrumors 6502a

    mathcolo

    Joined:
    Sep 14, 2008
    Location:
    Massachusetts
    #5
    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.
     

Share This Page