Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the contents in Cache directory?

In my cache directory i have a folder name Example. In that Example folder I am having sub folders CSS, JS. Now I want to remove the CSS subfolder. I am not getting

I used the below written code. If I use this my entire Example folder is getting removed.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *cacheFolderPath = [NSSearchPathForDirectoriesInDomains(NSCacheDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSError *error = nil;

for (NSString *fileName in [fileManager contentsOfDirectoryAtPath:cacheFolderPath error:&error])

{
    [fileManager removeItemAtPath:[cacheFolderPath stringByAppendingPathComponent:fileName] error:&error];

}

can any one help me.

like image 401
user1903049 Avatar asked Dec 14 '12 05:12

user1903049


1 Answers

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cacheFolderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
NSString *dir = [cacheFolderPath stringByAppendingPathComponent:@"yourCSSSUBFolderName"];
[[NSFileManager defaultManager] removeItemAtPath:dir error:nil];

You can do the same for any other directories you want to delete!

Later if you want to add those files than first create that subfolder and than add your files for e-g:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *dirPath=[directory stringByAppendingPathComponent:@"yourCSSSUBFolderName"];
NSFileManager *fileManger=[NSFileManager defaultManager];
if(![fileManger fileExistsAtPath:dirPath])
{
    NSError *error = nil;
    [fileManger createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
}
// now add the directories
NSString *filePath = [dirPath stringByAppendingPathComponent:@"Yourfile.extension"];
[yourData writeToFile:filePath atomically:YES]; //Write to file
like image 193
Asif Mujteba Avatar answered Sep 18 '22 04:09

Asif Mujteba