如何删除app指定目录中的所有文件?

时间:2023-01-14 23:30:34

Given a directory [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"] how do I delete ALL FILES in this folder?

给定一个目录[[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]如何删除该文件夹中的所有文件?

(assume a correct documents directory path)

(假设文件路径正确)

5 个解决方案

#1


113  

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.

如果错误存在,我让你来做一些有用的事情。

#2


16  

if you want to remove files and the directory itself then use it without for loop

如果您想删除文件和目录本身,那么使用它而不使用for循环

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
}

#3


12  

same for swift lovers:

同样的斯威夫特爱好者:

let fm = FileManager.default
do {
  let folderPath = "...my/folder/path"
  let paths = try fm.contentsOfDirectory(atPath: folderPath)
  for path in paths
  {
    try fm.removeItem(atPath: "\(folderPath)/\(path)")
  }
} catch {
  print(error.localizedDescription)
}

#4


0  

Swift 4

斯威夫特4

  do {

        let destinationLocation:URL = ...

        if FileManager.default.fileExists(atPath: destinationLocation.path) {
            try! FileManager.default.removeItem(at: destinationLocation)
        }

    } catch {
    print("Error \(error.localizedDescription)")
    }

#5


0  

Most of the older answers have you use contentsOfDirectoryAtPath:error: which will work, but according to Apple:

大多数老版本的答案都使用了contentsOfDirectoryAtPath:error:这是可行的,但是根据苹果的说法:

"The preferred way to specify the location of a file or directory is to use the NSURL class"

“指定文件或目录位置的首选方法是使用NSURL类”

so if you want to use NSURL instead you can use the method contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: so it would look something like this:

因此,如果你想使用NSURL,你可以使用contentsOfDirectoryAtURL的方法:includingpropertiesforkey:options:error:所以它看起来是这样的:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray<NSURL*> *urls = [fileManager contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];

    for (NSURL *url in urls)
    {
        NSError *error = nil;
        BOOL success = [fileManager removeItemAtURL:url error:error];
        if (!success || error) {
            // something went wrong
        }
    }

#1


113  

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.

如果错误存在,我让你来做一些有用的事情。

#2


16  

if you want to remove files and the directory itself then use it without for loop

如果您想删除文件和目录本身,那么使用它而不使用for循环

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
}

#3


12  

same for swift lovers:

同样的斯威夫特爱好者:

let fm = FileManager.default
do {
  let folderPath = "...my/folder/path"
  let paths = try fm.contentsOfDirectory(atPath: folderPath)
  for path in paths
  {
    try fm.removeItem(atPath: "\(folderPath)/\(path)")
  }
} catch {
  print(error.localizedDescription)
}

#4


0  

Swift 4

斯威夫特4

  do {

        let destinationLocation:URL = ...

        if FileManager.default.fileExists(atPath: destinationLocation.path) {
            try! FileManager.default.removeItem(at: destinationLocation)
        }

    } catch {
    print("Error \(error.localizedDescription)")
    }

#5


0  

Most of the older answers have you use contentsOfDirectoryAtPath:error: which will work, but according to Apple:

大多数老版本的答案都使用了contentsOfDirectoryAtPath:error:这是可行的,但是根据苹果的说法:

"The preferred way to specify the location of a file or directory is to use the NSURL class"

“指定文件或目录位置的首选方法是使用NSURL类”

so if you want to use NSURL instead you can use the method contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: so it would look something like this:

因此,如果你想使用NSURL,你可以使用contentsOfDirectoryAtURL的方法:includingpropertiesforkey:options:error:所以它看起来是这样的:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray<NSURL*> *urls = [fileManager contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];

    for (NSURL *url in urls)
    {
        NSError *error = nil;
        BOOL success = [fileManager removeItemAtURL:url error:error];
        if (!success || error) {
            // something went wrong
        }
    }