Following code will help you to add or delete file from document directory.
-(NSString *)saveFileWithOldName:(NSString*)oldNameStr
{
NSArray *dirPaths;
NSString *docsDirs;
NSString *tempStr2;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDirs = [dirPaths objectAtIndex:0];
docsDirs = [docsDirs stringByAppendingPathComponent:folderrName];
NSString *tempSoundPath;
NSString *soundFilePath = [docsDirs
stringByAppendingPathComponent:oldNameStr];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:soundFilePath])
{
int i = 1;
NSString *tempStr = [oldNameStr stringByReplacingOccurrencesOfString:@".caf" withString:@""];
//NSLog(@"saveFileWithOldName:tempStr:%@",tempStr);
tempStr2 = [tempStr stringByAppendingFormat:@"-%d.caf",i];
//NSLog(@"saveFileWithOldName:tempStr2:%@",tempStr2);
tempSoundPath = [docsDirs
stringByAppendingPathComponent:tempStr2];
//NSLog(@"saveFileWithOldName:tempSoundPath:%@",tempSoundPath);
while ([fileManager fileExistsAtPath:tempSoundPath])
{
i++;
tempStr2 = [tempStr stringByAppendingFormat:@"-%d.caf",i];
//NSLog(@"saveFileWithOldName:tempStr2:%@",tempStr2);
tempSoundPath = [docsDirs
stringByAppendingPathComponent:tempStr2];
//NSLog(@"saveFileWithOldName:tempSoundPath:%@",tempSoundPath);
}
// rename File
[fileManager moveItemAtPath:[docsDirs stringByAppendingPathComponent:oldNameStr] toPath:[docsDirs stringByAppendingPathComponent:tempStr2] error:NULL];
}
return tempSoundPath;
}
-(void)deleteFile:(NSString *)str
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirPaths;
NSString *docsDirs;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDirs = [dirPaths objectAtIndex:0];
NSString *temp = [docsDirs stringByAppendingPathComponent:str];
if ([fileManager fileExistsAtPath:temp])
{
[fileManager removeItemAtPath:temp error:nil];
}
}
0 Comment(s)