Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:36:23+00:00 2026-06-12T11:36:23+00:00

For using copyItemAtPath method of NSFileManager, if exists the same file or folder, occur

  • 0

For using “copyItemAtPath” method of NSFileManager, if exists the same file or folder, occur error.

Is there the replace feature in NSFileManager?

(If exists the same file, delete and copy If not, just copy.)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self generateTableContents];

}


- (void)generateTableContents {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *appsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [appsDirectory objectAtIndex:0];

    [fileManager changeCurrentDirectoryPath:documentPath];
    [fileManager createDirectoryAtPath:@"user_List1" withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *bundlePath = [[NSBundle mainBundle] resourcePath];

    NSError *error; // to hold the error details if things go wrong
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"IMG_0523" ofType:@"JPG"];

    if ([fileManager copyItemAtPath:sourcePath toPath: @"./user_List1/newIMG_0523.JPG" error:&error]) {
        NSLog(@"Copy complete!");
    } else {
        NSLog(@"Copy Failed : %@", error);
    }


    if ([fileManager copyItemAtPath:@"./user_List1" toPath: @"./user_List2" error:&error]) {
        NSLog(@"Copy complete!");
    } else {
        NSLog(@"Copy Failed : %@", error);
    }



}

Excuted result is complete.

enter image description here

/Users/nice7285/Library/Caches/appCode10/DerivedData/tempPrg-dd15264d/Build/Products/Debug-iphonesimulator/tempPrg.app/tempPrg
Simulator session started with process 1184
2012-10-04 06:28:32.653 tempPrg[1184:11303] Copy complete!
2012-10-04 06:28:32.672 tempPrg[1184:11303] Copy complete!

Process finished with exit code 143

But When already exist folder.

enter image description here

Result is fail.

2012-10-04 06:48:31.488 tempPrg[1243:11303] Copy Failed
2012-10-04 06:48:31.497 tempPrg[1243:11303] Copy Failed
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:36:25+00:00Added an answer on June 12, 2026 at 11:36 am

    You need to do an atomic save, for example:

    NSData *myData = ...; //fetched from somewhere
    [myData writeToFile:targetPath atomically:YES];
    

    But since you’re using copyItemAtPath, I have no idea if atomically works with it so I would just go for the quick dirty hack:

    if ([fileManager fileExistsAtPath:txtPath] == YES) {
        [fileManager removeItemAtPath:txtPath error:&error]
    }
    
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
    

    Sources: IOS: copy a file in documents folder

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this method to copy a file: [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error]; I want
in my iPhone projects, I am currently copying files using NSFileManager 's -copyItemAtPath:toPath:error: method.
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I'm using the following snippet of code to attempt to copy a file from
Using Microsoft SQL Server 2005, is there any way to see when a table
I am trying to copy a couple of image files from my MainBundle/SampleImages folder
Using the Qt library, is there a way to automatically update a QDateTimeEdit using
Using Java,I have to fetch multiple sets of values from an XML file to
I'm using the famous :) function createEditableCopyOfDatabaseIfNeeded for create an editable copy of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.