Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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
 
You can use a combination of the unlink() and rmdir() system calls:

Code:
$ man 2 unlink
$ man 2 rmdir
 
Can i use removefileatpath? If i cant how do i do what you suggested?
 
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
 
Still can't get it to work? Someone please help!

Code:
if ([[NSFileManager defaultManager] fileExistsAtPath:@"mypathtofileordir"]) {
		[[NSFileManager defaultManager] removeItemAtPath:@"mypathtofileordir" error:&error];
		NSLog(@"%@",error);
	}
 
Code:
if ([[NSFileManager defaultManager] fileExistsAtPath:@"mypathtofileordir"]) {
		[[NSFileManager defaultManager] removeItemAtPath:@"mypathtofileordir" error:&error];
		NSLog(@"%@",error);
	}

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.
 
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
 
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.
 
my code:
Code:
NSManager *manager;
[manager removeItemAtPath:@"/Desktop/FileTest"];

If my code is way off I apologize in advance. :)
 
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.
 
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
 
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

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


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

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.
 
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];
			}
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.