Có cách nào dễ dàng để xóa tất cả các tệp (hình ảnh) tôi đã lưu trong thư mục tài liệu của ứng dụng không?Xóa tất cả các tệp trong hộp cát iPhone (thư mục tài liệu)?
Trả lời
NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
...
}
}
} else {
// Error handling
...
}
nó đã làm việc cho hầu hết các phần. nhưng hóa ra nó removeItemAtPath yêu cầu đường dẫn tệp đầy đủ. vì vậy tôi đã sử dụng số này NSString * myFilePath = [documentsDir stringByAppendingPathComponent: path]; cảm ơn. –
Đúng vậy, Jinah. Lỗi của tôi. Tôi sẽ sửa mã ở trên. –
Nếu điều này làm việc, nó sẽ là tốt đẹp để loại bỏ các 'Untested code:' bình luận. Cảm ơn – Warpspace
- (void)removeFile
{
// you need to write a function to get to that directory
NSString *filePath = [self getDirectory];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
{
NSError *error;
if (![fileManager removeItemAtPath:filePath error:&error])
{
NSLog(@"Error removing file: %@", error);
};
}
}
Tôi tin rằng đây là ngắn hơn.
Dường như điều này chỉ xóa một tệp hoặc thư mục – Warpspace
Mã không hoạt động với iOS 7 và Xcode 5 để chỉnh sửa để hoạt động với ứng dụng của tôi. Khoản tín dụng lớn cho @Ole Begemann và @pablasso.
-(void)EmptySandbox
{
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
while (files.count > 0) {
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
if (!removeSuccess) {
// Error
}
}
} else {
// Error
}
}
}
Nếu bạn đang sử dụng CoreData, chỉ cần lưu ý điều này sẽ xóa Mô hình của bạn cũng – BenB
Cảm ơn bạn, mã này hoạt động như một nét duyên dáng. Tôi là một newbie và tôi xảy ra để ghi đè lên lưu trữ được lưu trữ NSMutableArray với NSMutableDictionary trên iPad. Tiết kiệm cuộc sống của tôi – nuttynibbles
NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
NSError *error = nil;
NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
...
}
}
} else {
// Error handling
...
}
Nó có thể không được áp dụng trong mọi trường hợp nhưng nói chung nó sẽ hiệu quả hơn để đưa tất cả các file trong một thư mục tùy chỉnh bên trong Documents Directory và sau đó sử dụng removeItemAtPath:error:
để xóa thư mục đó và tạo ra nó một lần nữa. Ví dụ:
// Clear cache
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:cacheDirectory error:&error];
if (error)
{
// error handling
}
// Create cache directory again
NSError *error2;
[[NSFileManager defaultManager] createDirectoryAtPath:cacheDirectory withIntermediateDirectories:YES attributes:nil error:&error2];
if (error2)
{
// error handling
}
Đối với các nhà phát triển Swift:
let fileMgr = NSFileManager()
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
if let directoryContents = try? fileMgr.contentsOfDirectoryAtPath(dirPath)
{
for path in directoryContents
{
let fullPath = (dirPath as NSString).stringByAppendingPathComponent(path)
do
{
try fileMgr.removeItem(atPath: fullPath)
print("Files deleted")
}
catch let error as NSError
{
print("Error deleting: \(error.localizedDescription)")
}
}
}
Swift 2.1 với một số phụ của:
do {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let documentsDirectoryURL = NSURL(fileURLWithPath: paths[0])
let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(documentsDirectory)
var totalMegaBytes: Double = 0
var nrOfFiles = 0
for filename in directoryContents {
let file = documentsDirectoryURL.URLByAppendingPathComponent(filename)
let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(file.path!)
let fileSize = fileAttributes[NSFileSize] as! Double
totalMegaBytes += fileSize/1024/1024
do {
try NSFileManager.defaultManager().removeItemAtURL(file)
nrOfFiles++
}catch let error as NSError{
print("> Emptying sandbox: could not delete file", filename, error)
}
}
print("> Emptying sandbox: Removed \(nrOfFiles) files with a total of \(round(totalMegaBytes))MB")
}catch let error as NSError{
print("> Emptying sandbox: Error emptying sandbox", error)
}
Tôi nhận được "lỗi được ném từ đây không được xử lý vì việc thu thập kèm theo không đầy đủ" – Luda
Tôi đang upvoting bạn. Nhưng bạn thực sự nên xem xét việc loại bỏ điều này: "hãy để lỗi như NSError". Hoặc có thể có một số giải pháp khác cho lỗi trên – Luda
Tôi không nhận được cảnh báo đó. Không phải NSError là đầy đủ nhất có thể? – Sam
- 1. iPhone SDK Xcode - Cách lấy tất cả các tên tập tin trong thư mục tài liệu
- 2. Xóa tất cả các tệp git khỏi một thư mục?
- 3. Buộc xóa tất cả các tệp khỏi một thư mục
- 4. Truy xuất tất cả tài liệu từ tất cả thư mục con trong thư viện tài liệu - CSOM
- 5. Xóa tất cả các tập tin trong một thư mục
- 6. Xóa thư mục và tất cả các tệp/thư mục con
- 7. Sao chép thư mục từ thư mục Tài nguyên iPhone vào thư mục tài liệu
- 8. cách xóa tất cả các tệp trừ ba tệp mới nhất trong một thư mục
- 9. linux bash - xóa tất cả các tệp nằm trong một thư mục từ một thư mục khác
- 10. Cách gzip tất cả các tệp trong tất cả các thư mục con trong bash
- 11. Liệt kê tất cả các tệp và thư mục trong thư mục + thư mục con
- 12. Mở tất cả các tệp trong một thư mục
- 13. Tìm tệp trong tất cả các thư mục có thể?
- 14. Cách nén tất cả các tệp trong thư mục
- 15. CoffeeScript - biên dịch tất cả các tệp .coffee trong thư mục hiện tại và tất cả các thư mục con
- 16. Làm thế nào để có được Tất cả các đường dẫn cho các tệp trong thư mục Tài liệu?
- 17. Bash. Xóa tất cả các tệp và thư mục nhưng một số tệp nhất định
- 18. Làm thế nào để liệt kê tất cả các thư mục và thư mục con/tệp trong iPhone SDK?
- 19. Thư mục tài liệu iPhone và UIFileSharingEnabled, ẩn các tài liệu nhất định
- 20. xóa tất cả các tệp có kích thước bằng không trong thư mục được chỉ định
- 21. Mã Python để tìm tất cả các tệp mới được tạo, sửa đổi và xóa trong tất cả các thư mục/thư mục con bắt đầu từ/directory
- 22. Liệt kê tất cả các thư mục trong thư mục
- 23. Lấy mảng thư mục trong thư mục tài liệu
- 24. Sử dụng tìm để xóa tất cả các thư mục con (và các tệp của họ)
- 25. PHP: Hủy liên kết tất cả các tệp trong thư mục, và sau đó xóa thư mục đó
- 26. Lệnh Unix để xóa tất cả các tệp trong một thư mục nhưng giữ lại thư mục
- 27. Cách xóa tất cả các thư mục có tên x trong thư mục bằng tệp cmd/batch
- 28. grep trong tất cả các thư mục
- 29. Java tải xuống tất cả các tệp và thư mục trong một thư mục
- 30. tải hình ảnh từ thư mục tài liệu trong iPhone
http://stackoverflow.com/questions/4792663/remote-wipe-of- application-data-in-ios đây là một câu hỏi tương tự giả định anh ta muốn xóa mọi thứ trong thư mục tài liệu, nhưng không có giải pháp nào được nêu ra là –