Given a directory [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]
how do I delete ALL FILES in this folder?
(assume a correct documents directory path)
Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.
Navigate to the folder that you want to delete (with all its files and subfolders). Use cd *path*, for example, cd C:\Trash\Files\ to do so. Use cd .. to navigate to the parent folder and run the command RMDIR /Q/S *foldername* to delete the folder and all of its subfolders.
NSFileManager *fm = [NSFileManager defaultManager]; NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]; NSError *error = nil; for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) { BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error]; if (!success || error) { // it failed. } }
I leave it up to you to do something useful with the error if it exists.
if you want to remove files and the directory itself then use it without for
loop
NSFileManager *fm = [NSFileManager defaultManager]; NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos"]; NSError *error = nil; BOOL success = [fm removeItemAtPath:cacheImageDirectory error:&error]; if (!success || error) { // something went wrong }
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