How To Change a File Name?

Discussion in 'iOS Development' started by Cokegod, May 15, 2010.

  1. Cokegod

    Cokegod Member

    Joined:
    Aug 6, 2009
    Messages:
    332
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Hello, I want to make an app that can change file names, you know, that I will give the path for the file and the name I want it to be, and then it would change as I written.
    I can make the GUI, but what command should I make the button to make, I mean, there is a code that the button press triggers, so what should I write there for what I want to work?
    (Of course this app should be for jailbroken devices only)
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    you can use the C stdlib function rename(), which takes two arguments of type const char * and returns 0 if successful.

    also, for a Cocoa solution, use -[NSFileManager moveItemAtPath:toPath:error:]

    the documentation should be able to answer any questions about arguments or return value that you may have
  3. Cokegod

    Cokegod Member

    Joined:
    Aug 6, 2009
    Messages:
    332
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Okay, I have tried to use the NSFileManager a bit, and I wrote this code:
    Code:
    NSError *error;
    	NSFileManager *fileMgr = [NSFileManager defaultManager];
    	[fileMgr moveItemAtPath:@"/private/test/test.png" toPath:@"/private/test/test2.png" error:&error];
    And this is not working, can you tell me what the problem is?
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You probably don't have permission to write to that dir.
  5. Cokegod

    Cokegod Member

    Joined:
    Aug 6, 2009
    Messages:
    332
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Well, so how do I give my app permission to write to that dir, because I can move files to that dir with SSH...
  6. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    When you move with SSH, you are doing it as root, the app runs as mobile.

    Try renaming a file in /var/mobile.

    Also, your app can't be sandboxed, so if you are installing directly with XCode, it's gonna fail, you need to drag the compiled .app to /Applications.
  7. Cokegod

    Cokegod Member

    Joined:
    Aug 6, 2009
    Messages:
    332
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Ah, ok, but actually the idea of the app is renaming a file in /System/Library/CoreServices/Springboard.app
    How can I do that?

Share This Page