Using The Document Directory To Store Files

Show contents of Documents directory


	NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

Does File Exist


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
	path = [path stringByAppendingPathComponent:@"SomeFileName"];
	if ([[NSFileManager defaultManager] fileExistsAtPath:path])
	{

Delete File


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectory"];
	path = [path stringByAppendingPathComponent:@"SomeFileName"];
	NSError *error;
	if ([[NSFileManager defaultManager] fileExistsAtPath:path])		//Does file exist?
	{
		if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])	//Delete it
		{
			NSLog(@"Delete file error: %@", error);
		}
	}

Delete DIrectory


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	NSError *error;	

	if ([[NSFileManager defaultManager] fileExistsAtPath:path])	//Does directory exist?
	{
		if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])	//Delete it
		{
			NSLog(@"Delete directory error: %@", error);
		}
	}

Create Directory


	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	NSError *error;
	if (![[NSFileManager defaultManager] fileExistsAtPath:path])	//Does directory already exist?
	{
		if (![[NSFileManager defaultManager] createDirectoryAtPath:path
									   withIntermediateDirectories:NO
														attributes:nil
															 error:&error])
		{
			NSLog(@"Create directory error: %@", error);
		}
	}

Save NSData File


	NSData *file;
	file = ...
	NSString *path;
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
	path = [path stringByAppendingPathComponent:@"SomeFileName"];

        [[NSFileManager defaultManager] createFileAtPath:path
                                                        contents:FileData
                                                      attributes:nil];

You can test the bool result of createFileAtPath if desired. A common reason for it to fail is if you are writing to a directory that doesn’t exist – you have to create the directory first.

Load File


    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"My Directory"];
    path = [path stringByAppendingPathComponent:@"My Filename"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        //File exists
        NSData *file1 = [[NSData alloc] initWithContentsOfFile:path];
        if (file1)
        {
            [self MyFunctionWantingAFile:file1];
            [file1 release];

        }
    }
    else
    {
        NSLog(@"File does not exist");
    }

Move File


	[[NSFileManager defaultManager] moveItemAtPath:MySourcePath toPath:MyDestPath error:nil];

Rename File

Use same method as moving a file

List Files


    //----- LIST ALL FILES -----
    NSLog(@"LISTING ALL FILES FOUND");

    int Count;
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    for (Count = 0; Count < (int)[directoryContent count]; Count++)
    {
        NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]);
    }
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.
comments powered by Disqus
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.