4

I am saving my files in Document directory named 1.png, 2.png, 3.png, 4.png, 5.png. If I delete the original file 3.png, how do I rename files 4.png and 5.png to be called 3.png and 4.png respectively?

This is the code I am using to write the files in the first place:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[appDel.photo addObject:@"photo"];
NSString *imageName = [NSString stringWithFormat:@"%i.png", [appDel.photo count]];
appDel.numberPhoto = [appDel.photo count];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  imageName];
NSData* data = UIImagePNGRepresentation(image); 
[data writeToFile:path atomically:YES];
iain
  • 5,660
  • 1
  • 30
  • 51
Nubaslon
  • 741
  • 2
  • 11
  • 27
  • Have you tried to write any code that would do this for you ? – rckoenes Mar 28 '13 at 12:48
  • This is the point. I do not know how to get the file and renaming it ((( and I do not know how to write this code – Nubaslon Mar 28 '13 at 12:51
  • If you are saving the files through code to your directory then through the same method you can rename them. Just over write the name of the file in the method. – Adrian P Mar 28 '13 at 12:52
  • How do you save the files? Plist, SQL, what is the method of saving? – Adrian P Mar 28 '13 at 12:53

1 Answers1

2

To get NSDocuments directory use :

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyFile.txt"];

How to rename file you can find here:

How to rename a file using NSFileManager

If you do not know the names of the files in documents directory you can use :

NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

This array contains all filenames you need. So you can rename them all.

Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109