Deleting a file in Objective-C

Discussion in 'Mac Programming' started by Blakeasd, Jul 15, 2010.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello All!

    I am currently writing a program that deletes a folder from your computer. How can I do this? Do I use the filemanager:removefileatpath method? If so I only know how to direct it to files in the programs path. How do I delete a file, say, on the desktop? Thanks in advance!
    Blake
     
  2. ThaGrapist macrumors newbie

    Joined:
    Feb 13, 2010
    Location:
    NC
    #2
    You can use a combination of the unlink() and rmdir() system calls:

    Code:
    $ man 2 unlink
    $ man 2 rmdir
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    Can i use removefileatpath? If i cant how do i do what you suggested?
     
  4. ThaGrapist macrumors newbie

    Joined:
    Feb 13, 2010
    Location:
    NC
    #4
  5. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    So you know the name of a method to use, and the class it goes with but not how to make it work?
     
  6. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    Yea, I know it sounds weird. I know how to delete file in the programs path but I just need help deleting a file that isn't in the programs path. Please Help
     
  7. artbeat.easy macrumors newbie

    Joined:
    Jul 1, 2010
    #7
    Code:
    if ([[NSFileManager defaultManager] fileExistsAtPath:@"mypathtofileordir"]) {
    		[[NSFileManager defaultManager] removeItemAtPath:@"mypathtofileordir" error:&error];
    		NSLog(@"%@",error);
    	}
     
  8. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #8
    This is prone to race conditions (i.e. the file's existence changes between the if and the contents of the if). I would just go ahead and remove the file, then handle the error if it occurs.
     
  9. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    None of this is working :confused: . Here is what I am trying to do. I have a program with a button. When the users clicks the button it deletes a file on the desktop called FileTest . How do I acheieve this in Objective-C. Please make a step by step answer is possible. Sorry if I am confusing you, just a little frustrated. I already have the button linked to the code and stuff like that I just need help deleting the file
     
  10. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #10
    Post your code. Your question has already been answered, but apparently you can't understand the answer well enough to apply it to your situation.
     
  11. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #11
    my code:
    Code:
    
    NSManager *manager;
    [manager removeItemAtPath:@"/Desktop/FileTest"];
    
    
    If my code is way off I apologize in advance. :)
     
  12. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #12
    Waaaay off. Your issue isn't "deleting files" it's "basic understanding of objective-c".

    Some problems:
    1) There's no such thing as NSManager
    2) You never initialize the manager variable

    I suggest reading the code that artbeat.easy posted.
     
  13. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #13
    I meant to put NSFileManager, sorry. How exactly do you initialize it? I was't aware you had to initialize the manager. Sorry if im frustrating you. I copied his code and it said error was undeclared. Can someone post an example with my scenario and file names
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    3) The pathname of the file to delete is wrong.
    "/Desktop/FileTest" is NOT a file residing in the user's Desktop folder. You have confused absolute and relative pathnames.
    http://en.wikipedia.org/wiki/Path_(computing)
    http://en.wikipedia.org/wiki/Working_directory


    When you get an error, post the text of the error.

    Just because you didn't get an error with your code doesn't mean you've fixed anything.

    Frankly, I think you're in over your head and are trying to hack your way through. This almost never works.

    You need to go back and figure out what to do when someone gives you a code fragment, rather than a step-by-step specific example. That means you have to understand the Objective-C language better, because copy-and-paste along isn't going to cut it.

    You should also refer to the reference docs for the class given, so you understand what the parameters mean, and what's needed. Just hacking at it and hoping it will work is a huge waste of time and effort.
     
  15. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #15
    Here is my new code but it still doesn't work? Can someone helP?


    Code:
    if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Users/Blake/Desktop"]) {
    		[[NSFileManager defaultManager] removeItemAtPath:@"FileTest" error:nil];
    			}
    
     
  16. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #16
    You need to use a full path the second time as well. @"/Users/Blake/Desktop/FileTest" for example. Or, more portably, [NSHomeDirectory() stringByAppendingPathComponent:mad:"Desktop/FileTest"]; ;)

    Also, as I suggested above, I wouldn't bother checking if the file exists before trying to delete it.
     

Share This Page