Get File Size
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"SomeDirectoryName"];
URL = [URL stringByAppendingPathComponent:@"MyFileName.txt"];
NSError *AttributesError = nil;
NSDictionary *FileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&AttributesError];
NSNumber *FileSizeNumber = [FileAttributes objectForKey:NSFileSize];
long FileSize = [FileSizeNumber longValue];
NSLog(@"File: %@, Size: %ld", URL, FileSize);
Delete All Files In Directory
//DELETE ALL FILES IN DIRECTORY
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MyDirectoryName"];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSError *error = nil;
if (error == nil)
{
for (NSString *Filename in directoryContent)
{
NSString *FilePath = [path stringByAppendingPathComponent:Filename];
BOOL removeSuccess = [[NSFileManager defaultManager] removeItemAtPath:FilePath error:&error];
if (!removeSuccess)
{
// Error handling
}
}
}
Create Incrementing Numeric Value File
//###########################################
//##### CREATE INCREMENTING NUMBER FILE #####
//###########################################
//Create array of data values
UInt8 DataBytes[4];
NSMutableData *FileData = [[NSMutableData alloc] init];
UInt32 DataIndex = 0;
for (DataIndex = 0; DataIndex < (1024 * 1024 * 2); DataIndex += 4)
{
DataBytes[0] = (Byte)((DataIndex & 0xff000000) >> 24);
DataBytes[1] = (Byte)((DataIndex & 0x00ff0000) >> 16);
DataBytes[2] = (Byte)((DataIndex & 0x0000ff00) >> 8);
DataBytes[3] = (Byte)(DataIndex & 0x000000ff);
[FileData appendBytes:DataBytes length:4];
}
//Save to file
NSString *DestFilename = @"test_file.txt";
NSString *DestPath;
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
DestPath = [[Paths objectAtIndex:0] stringByAppendingPathComponent:@"MyPathName"];
DestPath = [DestPath stringByAppendingPathComponent:DestFilename];
if (![[NSFileManager defaultManager] fileExistsAtPath:DestPath])
{
NSLog(@"Generating dummy file: %@", DestFilename);
[[NSFileManager defaultManager] createFileAtPath:DestPath
contents:FileData
attributes:nil];
}
[FileData release];