S.J. Lim
S.J. Lim

Reputation: 3165

How to replace a file or folder by using NSFileManager?

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

Upvotes: 2

Views: 8317

Answers (2)

Daniel
Daniel

Reputation: 1057

Starting with iOS 4 (and macOS 10.7) there is an explicit method for replacing one item with another. It even keeps the properties of the original file (optional)

- (BOOL)replaceItemAtURL:(NSURL *)originalItemURL withItemAtURL:(NSURL *)newItemURL backupItemName:(NSString *)backupItemName options:(NSFileManagerItemReplacementOptions)options resultingItemURL:(NSURL * _Nullable *)resultingURL error:(NSError * _Nullable *)error;

Replaces the contents of the item at the specified URL in a manner that ensures no data loss occurs.

replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:

Upvotes: 1

Luke
Luke

Reputation: 5104

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

Upvotes: 9

Related Questions