0

I am trying to create two log files and replace content in the second file with the content in the first file.

My code for creating log in AppDelegate

   NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory3 = [paths3 objectAtIndex:0];
       logPath3 = [documentsDirectory3 stringByAppendingPathComponent:@"console4.log"];

        if(![[NSFileManager defaultManager] fileExistsAtPath:logPath3])
            [[NSFileManager defaultManager] createFileAtPath:logPath3 contents:[NSData data] attributes:nil];

        NSLog(@"path %@",logPath3);

        freopen([logPath3 cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);

 NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
        logPath2 = [documentsDirectory2 stringByAppendingPathComponent:@"console3.log"];

        if(![[NSFileManager defaultManager] fileExistsAtPath:logPath2])
            [[NSFileManager defaultManager] createFileAtPath:logPath2 contents:[NSData data] attributes:nil];

        NSLog(@"path %@",logPath2);

        freopen([logPath2 cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
        _isFileOneCreated =YES;

code to move content

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];

    NSError * err;
if ([filemgr moveItemAtPath:
     logPath2 toPath:
   logPath3 error: &err])
    NSLog (@"Day 3 Move successful");
else
    NSLog (@"Day 3 Move failed  %@",[err localizedDescription]);

But i get an error 516.

   (Cocoa error 516.)" UserInfo=0x16dba220    {NSSourceFilePathErrorKey=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console3.log, NSUserStringVariant=(
Move
), NSFilePath=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console3.log, NSDestinationFilePath=/var/mobile/Containers/Data/Application/274E9843-2C8E-45F3-BD41-EA392F50C7AC/Documents/console4.log, NSUnderlyingError=0x16da63f0 "The operation couldn’t be completed. File exists"}

Help me solve my issue

vishnuvarthan
  • 482
  • 1
  • 6
  • 22
  • Well what do you expect when you use two different ways of getting the paths? – borrrden Oct 15 '14 at 04:26
  • You can't just start out using "Documents/...". Use [this](http://stackoverflow.com/questions/272544/whats-the-best-way-to-find-the-users-documents-directory-on-an-iphone). – rebello95 Oct 15 '14 at 04:26
  • @borrrden help me mate i am new with file manager – vishnuvarthan Oct 15 '14 at 04:27
  • Do the same thing you did in the first chunk of code to get the directory again – borrrden Oct 15 '14 at 04:28
  • can you post the code – vishnuvarthan Oct 15 '14 at 04:29
  • @vishnuvarthan You already posted the code, use the same thing. Look at `paths2` to see what we mean. – rebello95 Oct 15 '14 at 04:35
  • Please post the logs too. Did that work? – rebello95 Oct 15 '14 at 04:37
  • Day 3 Move failed The operation couldn’t be completed. (Cocoa error 516.) @rebello95 .This also did not work – vishnuvarthan Oct 15 '14 at 04:41
  • Check the contents of _Document_ directory; error says _file_ _exist_ which explains as _moveItemAtPath_ will abort the move operation if file is present at destination path, you may want to keep a check for that. If file exist, reset your simulator and try again. – Gandalf Oct 15 '14 at 05:13
  • Yes i have both the files already created and also has some content in both file(device logs).I need to replace the content @Gandalf – vishnuvarthan Oct 15 '14 at 05:16
  • you did open console2.log so fclose it before move. – larva Oct 15 '14 at 06:42
  • What is _logPath1_ in file movement code? Your error contains file name as _console3.log_ and _console4.log_, which i do not see in your code. Assuming that you want file1 to be overwritten by file2 you are doing a mistake as you are having both the file in same directory and then you want resulting file to be overwritten. So final scenario is like having 2 file with same name at same location. That's why the error. Remove the destination file and move the source file to new path. – Gandalf Oct 15 '14 at 06:44
  • @Gandalf see the edited code. Giving a sample code will be more helpfull – vishnuvarthan Oct 15 '14 at 10:32
  • So if you want to save your logs for 30 days can I assume you'll have 30 identical chunks of code, each performing (mostly) the same thing? I think you need to learn about loops. – Droppy Oct 15 '14 at 10:56
  • @Droppy i need all the recent 30 days files at any point of time so i need to have the recent thirty day files,which means i need to store thirty files – vishnuvarthan Oct 15 '14 at 11:22

1 Answers1

1

moveItemAtPath does not allow you to overwrite the file with same name. See this Question Thread.

All you need to do is delete the file at target location before you overwrite with new one. Use the below code. This will do the trick but i advice you to take precaution to have the backup of your file just in case move doesn't work for some unknown reason.

NSFileManager *filemgr = [NSFileManager defaultManager];   
NSError * err;  
[filemgr removeItemAtPath:logPath3 error:&err];  
if ([filemgr moveItemAtPath:logPath3 toPath:logPath2 error: &err])   
   NSLog (@"Day 3 Move successful");  
else   
   NSLog (@"Day 3 Move failed  %@",[err localizedDescription]);
Community
  • 1
  • 1
Gandalf
  • 2,164
  • 2
  • 13
  • 19