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]);
    }

Comments

  1. Getsy

    Where is the sample, how to store a file (like html, mp4 etc) from bundle to documents directory?