2011-04-26 16 views
11

मैं copyItemAtPath के साथ निर्देशिकाओं की प्रतिलिपि बनाने की कोशिश कर रहा हूं, फिर भी जब भी यह "ऑपरेशन पूर्ण नहीं हो सका। फ़ाइल मौजूद है" त्रुटि।copyItemAtPath हमेशा फाइल के साथ विफल रहता है त्रुटि

यहाँ कोड मैं उपयोग कर रहा हूँ लॉग का

NSLog(@"Copying from: %@ to: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"template_1_path"], path); 
if(![file_manager copyItemAtPath:[NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"template_1_path"]] toPath:[NSString stringWithFormat:@"%@", path] error:&error]) { 
     NSLog(@"%@" [error localizedDescription]); 
} 

उदाहरण है -

"Copying from: /Users/testuser/Sites/example_site to: /Users/testuser/Desktop" 
"The operation couldn’t be completed. File exists" 

मैं गलत क्या कर रही हूं पर कोई भी विचार?

अग्रिम धन्यवाद!

उत्तर

21

आप "/ उपयोगकर्ता/testuser/साइट्स/example_site" फ़ाइल को फ़ाइल "/ उपयोगकर्ता/testuser/डेस्कटॉप/example_site" फ़ाइल में कॉपी करने की कोशिश कर रहे हैं, यह मानते हुए कि आप केवल गंतव्य निर्देशिका निर्दिष्ट कर सकते हैं और यह उपयोग करेगा स्रोत फ़ाइल नाम। यह काम नहीं करता। Quoth the documentation:

जब एक फ़ाइल की प्रतिलिपि की जा रही है, गंतव्य पथ एक में समाप्त होना चाहिए फ़ाइल नाम-वहाँ स्रोत फ़ाइल नाम का कोई निहित गोद लेने है।

+0

यह चाल के लिए भी सच है ItemAtPath: ... और moveItemAtURL: ... – LaborEtArs

16

आप एक ही फ़ाइल नाम के साथ कुछ कॉपी करने की कोशिश कर रहे हैं। इस तरह कुछ कोशिश करें:

- (BOOL)copyFolderAtPath:(NSString *)sourceFolder toDestinationFolderAtPath:(NSString*)destinationFolder { 
    //including root folder. 
    //Just remove it if you just want to copy the contents of the source folder. 
    destinationFolder = [destinationFolder stringByAppendingPathComponent:[sourceFolder lastPathComponent]]; 

    NSFileManager * fileManager = [ NSFileManager defaultManager]; 
    NSError * error = nil; 
    //check if destinationFolder exists 
    if ([ fileManager fileExistsAtPath:destinationFolder]) 
    { 
     //removing destination, so soucer may be copied 
     if (![fileManager removeItemAtPath:destinationFolder error:&error]) 
     { 
      NSLog(@"Could not remove old files. Error:%@",error); 
      [error release]; 
      return NO; 
     } 
    } 
    error = nil; 
    //copying destination 
    if (!([ fileManager copyItemAtPath:sourceFolder toPath:destinationFolder error:&error ])) 
    { 
     NSLog(@"Could not copy report at path %@ to path %@. error %@",sourceFolder, destinationFolder, error); 
     [error release]; 
     return NO; 
    } 
    return YES; 
} 
संबंधित मुद्दे