Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2008, 02:05 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 9
simsimma is on a distinguished road
Default How do I delete a file in my documents directory?

Sorry I am a newb and am currently saving .gif files in my documents directory.

However, when a user deletes an object, I need to also delete the associated .gif files.

Can anyone point me to some code that will let me do that?
simsimma is offline   Reply With Quote
Old 09-09-2008, 04:34 PM   #2 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Create a reference to the default file manager and pass it the removeItemAtPath:error: message. In the code below, myPath is the path to the file you want to delete. The NULL value can be replaced with an NSError ** to get error info if there's any. If you do that, replace the NULL with &myNSErrorVariableName.

Code:
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:myPath error:NULL];
myersn024 is offline   Reply With Quote
Old 09-25-2008, 11:27 AM   #3 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
cope784 is an unknown quantity at this point
Default

Quote:
Originally Posted by myersn024 View Post
Create a reference to the default file manager and pass it the removeItemAtPath:error: message. In the code below, myPath is the path to the file you want to delete. The NULL value can be replaced with an NSError ** to get error info if there's any. If you do that, replace the NULL with &myNSErrorVariableName.

Code:
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:myPath error:NULL];
Myers, thanks for the help. I want to delete a .db file in the Apps Document Directory. Could you please show me in the "removeItemAtPath:" function how to select that exact .db file and path.

Thanks.
cope784 is offline   Reply With Quote
Old 09-25-2008, 02:18 PM   #4 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
rames44 is on a distinguished road
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

You can get the path to your Documents directory as follows:
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
You can then get the path to a specific file within the documents directory using:
Code:
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:@"whateverTheFileNameIs"];
I'm presuming that you know the name of the file you want to delete .
rames44 is offline   Reply With Quote
Old 09-25-2008, 03:37 PM   #5 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
cope784 is an unknown quantity at this point
Default

rames,

Thanks a lot for the advise and helpful code. I will try it out tomorrow and let you know how it goes.
cope784 is offline   Reply With Quote
Old 09-30-2008, 11:03 AM   #6 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
cope784 is an unknown quantity at this point
Default

rames,

I implemented your code, but it doesn't work. I believe that the error is in my project somewhere because the code you shared makes sense and should work. I will let you know when I figure it out.

Thanks.
cope784 is offline   Reply With Quote
Old 10-10-2008, 07:52 AM   #7 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
cope784 is an unknown quantity at this point
Default

Quote:
Originally Posted by rames44 View Post
You can get the path to your Documents directory as follows:
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
You can then get the path to a specific file within the documents directory using:
Code:
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:@"whateverTheFileNameIs"];
I'm presuming that you know the name of the file you want to delete .

Thanks again for your time. I figured out what was going on. We are creating another directory in the documents directory, (i.e. = \\documents\images) in which the files that I need to delete are located. I was trying to access the files in the documents directory not the secondary directory. I am trying to figure out how to access the secondary directory. I am new to low level file management and I will read through the documentation, though any help would be nice.
cope784 is offline   Reply With Quote
Old 10-10-2008, 08:02 AM   #8 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
cope784 is an unknown quantity at this point
Default

I actually think I figured it out. Here is my code to access the secondary directory in the documents directory.

Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
	NSString *secondaryDirectoryPath = [documentsDirectoryPath stringByAppendingPathComponent:@"secondary"];
	NSString *databaseFile = [secondaryDirectoryPath stringByAppendingPathComponent:@"database.db"];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	[fileManager removeItemAtPath:databaseFile error:NULL];
I implemented this code within a function and everything seems to be working. thanks again for the help.
cope784 is offline   Reply With Quote
Old 05-10-2011, 02:27 AM   #9 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

EDIT: sorry, i posted in the wrong thread...
__________________
dany_dev is offline   Reply With Quote
Old 01-05-2012, 05:12 AM   #10 (permalink)
Registered Member
 
Join Date: Sep 2010
Location: Ahmedabad
Posts: 25
nitingohel is on a distinguished road
Send a message via Skype™ to nitingohel
Default only selected item delete

Quote:
Originally Posted by dany_dev View Post
EDIT: sorry, i posted in the wrong thread...
hi..
how to remove only selected item remove from document directory ....???

like multiple choose items from view and delete at documents directory..
any idea...?
__________________
neet
www.iphoneadd.com
nitingohel is offline   Reply With Quote
Reply

Bookmarks

Tags
delete, file system, images

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 325
13 members and 312 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, Sloshmonster, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0