Fire Fist
Fire Fist

Reputation: 7050

How to copy text file from document directory in iOS

Hello i am trying to copy text file from document directory into folder name Temp that located in document directory.

Here is some of code i tried and didn't work.

self.fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentsDirectoryToTemp = [NSString stringWithFormat:@"%@/Temp/",[paths objectAtIndex:0]];

        NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",tblGetNameToMove.textLabel.text]];

        [self.fileManager copyItemAtPath:txtPath toPath:documentsDirectoryToTemp error:&error];

How can i do that?

Upvotes: 0

Views: 305

Answers (2)

Fire Fist
Fire Fist

Reputation: 7050

Here is solution. I need to add directory and also name of file and extension.

Here is codes.

self.fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",tblGetNameToMove.textLabel.text]];

        NSString *fileName = [NSString stringWithFormat:@"%@/Temp/%@.txt",documentsDirectory,tblGetNameToMove.textLabel.text];

        [self.fileManager copyItemAtPath:txtPath toPath:fileName error:&error];

Upvotes: 0

V-Xtreme
V-Xtreme

Reputation: 7323

There could be many reasons for this :

  1. First try to print all the path like documentsDirectoryToTempn and txtPath whether it is correct or not .

  2. The second thing is make use that error variable :

    if(error) { NSLog("Error while coping :%@",[error localizedDiscription]); }

    so you can track exact problem .

Upvotes: 1

Related Questions