2010-02-09 15 views
46

एक निर्देशिका को देखते हुए [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"] मैं इस फ़ोल्डर में सभी फाइलों को कैसे हटा सकता हूं?ऐप पर निर्दिष्ट निर्देशिका में सभी FILES को कैसे हटाएं?

(एक सही दस्तावेज निर्देशिका पथ मान)

उत्तर

111
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. 
    } 
} 

मैं यह आप पर निर्भर छोड़ त्रुटि के साथ थोड़ा उपयोगी यदि वह मौजूद है।

+1

@ m1neral:

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

ताकि आप NSURL का उपयोग करने के बजाय आप विधि contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: उपयोग कर सकते हैं तो यह कुछ इस तरह दिखेगा चाहते हैं

टिप्पणी, एक संपादन नहीं – abatishchev

+0

मैंने इसे वापस घुमाया क्योंकि यह वैसे भी गलत था। – coneybeare

+20

सामान्य रूप से, आपको पथ को संयोजित करने के लिए शायद 'stringWithFormat' के बजाय' stringByAppendingPathComponent' का उपयोग करना चाहिए। (मुझे उपर्युक्त कार्यों को पता है, लेकिन केवल आपके हार्ड कोडित पिछली स्लैश के कारण '@" फ़ोटो/"'।) – zekel

16

आप फ़ाइलें और निर्देशिका खुद को दूर करने के तो 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 
} 
12

तेज प्रेमियों के लिए एक ही चाहते हैं:

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) 
} 
+0

, के लिए मुझे धन्यवाद सिर्फ एक छोटे संशोधन की जरूरत में काम किया धन्यवाद –

0

स्विफ्ट 4

do { 

     let destinationLocation:URL = ... 

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

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

यह नहीं होना चाहिए प्रयास करने के बाद बल, अगर आप पकड़ का उपयोग कर रहे हैं। इसके अलावा, आप यूआरएल द्वारा फाइल को हटाने की कोशिश कर रहे हैं, न कि पथ से सभी फाइलें। –

+0

ऐप्पल से: "फ़ाइल सिस्टम की वर्तमान स्थिति या फ़ाइल सिस्टम पर किसी विशेष फ़ाइल के आधार पर व्यवहार की भविष्यवाणी करने का प्रयास करने की अनुशंसा नहीं की जाती है", जिसका अर्थ है कि आपको यह जांचना नहीं चाहिए कि फाइल इसे हटाने से पहले मौजूद है या नहीं। आपको बस फ़ाइल को आजमाएं और हटाएं और फिर उचित तरीके से त्रुटि को संभालना चाहिए। अधिक जानकारी के लिए 'fileExists (atPath:)' पर [प्रलेखन] (https://developer.apple.com/documentation/foundation/filemanager/1415645-fileexists) देखें। – strikerdude10

0

अधिकांश पुराने उत्तरों में आपका उपयोग करते हैं एक यह होना चाहिए:जो काम करेंगे, लेकिन according to Apple:

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 
     } 
    } 
संबंधित मुद्दे