Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming an existing file with Obj-C

Tags:

objective-c

I've seen this question asked a few times but I have been unable thus far to achieve success using any of the post solutions. What I am trying to do is rename a file in the local storage of an app (also kind of new to Obj-c). I am able to retrieve the old path and create the new path, but what would I have to write in order to actually change the files name?

What I have thus far is:

- (void) setPDFName:(NSString*)name{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES);
    NSString* initPath = [NSString stringWithFormat:@"%@/%@",[dirPaths objectAtIndex:0], @"newPDF.pdf"];
    NSString *newPath = [[NSString stringWithFormat:@"%@/%@",
                          [initPath stringByDeletingLastPathComponent], name]
                         stringByAppendingPathExtension:[initPath pathExtension]];
}
like image 237
Charles Lillo Avatar asked Jan 11 '13 15:01

Charles Lillo


People also ask

How do I change the name of an existing file?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.

How do I rename multiple files with sequential numbers?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

How do you change the name of a file with spaces?

If you want to rename a file name containing spaces to a new file name with spaces, place quotation marks around both file names, as in the following example. The same examples above can be applied to the copy, rename, delete, or other commands in the Windows command line that involve a file name with a space.

How do I change a file name in Xcode?

Select the project you want to rename in the “Project Navigator” which can be found on the left side of the Xcode view. On the right-hand side of the window, select the “File Inspector”. The name of your project should be in there under “Identity and Type”, change it to “NEW” and press Enter.


2 Answers

NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtPath:initPath toPath:newPath error:&error];
like image 106
CubeSchrauber Avatar answered Oct 09 '22 10:10

CubeSchrauber


The code is very messy; try this:

- (BOOL)renameFileFrom:(NSString*)oldName to:(NSString *)newName
{
    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES) objectAtIndex:0];
    NSString *oldPath = [documentDir stringByAppendingPathComponent:oldName];
    NSString *newPath = [documentDir stringByAppendingPathComponent:newName];

    NSFileManager *fileMan = [NSFileManager defaultManager];
    NSError *error = nil;
    if (![fileMan moveItemAtPath:oldPath toPath:newPath error:&error])
    {
        NSLog(@"Failed to move '%@' to '%@': %@", oldPath, newPath, [error localizedDescription]);
        return NO;
    }
    return YES;
}

and call this using:

if (![self renameFileFrom:@"oldName.pdf" to:@"newName.pdf])
{
    // Something went wrong
}

Better still, put the renameFileFrom:to: method into a utility class and make it a class method so it can be called from anywhere in your project.

like image 44
trojanfoe Avatar answered Oct 09 '22 09:10

trojanfoe