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 *filePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SomeDirectoryName"];
filePath = [path stringByAppendingPathComponent:@"SomeFileName];
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:file
attributes:nil];
Move File
[[NSFileManager defaultManager] moveItemAtPath:MySourcePath toPath:MyDestPath error:nil];
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]);
}