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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With