I found the code below to delete file in objective-c, but I want to only delete all files under directory of Caches
and keep the directory Caches
itself.
Could someone suggest the method to do that?
Thanks
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL] == YES)
NSLog (@"Remove successful");
else
NSLog (@"Remove failed");
UPDATED
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL] == YES)
NSLog (@"Remove successful");
else
NSLog (@"Remove failed");
[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
Loop through the files in that directory.
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:directory error:nil];
for (NSString *filename in fileArray) {
[fileMgr removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:NULL];
}
- (void) removeDocuments
{
NSString *docDir = // get documents directory
NSString *cacheDir = [docDir stringByAppendingPathComponent: @"cacheDir"];
// check if cache dir exists
// get all files in this directory
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *fileList = [fm contentsOfDirectoryAtPath: cacheDir error: nil];
// remove
for(NSInteger i = 0; i < [fileList count]; ++i)
{
NSString *fp = [fileList objectAtIndex: i];
NSString *remPath = [cacheDir stringByAppendingPathComponent: fp];
[fm removeItemAtPath: remPath error: nil];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.