使用NSFIleManager重命名和移动文件

时间:2012-01-24 07:45:56

标签: iphone objective-c ios

这是我的代码,用于重命名文件并将文件从临时目录移动到My Documents目录。

因此,问题出在模式的两个部分中创建目录并尝试移动。其余的都正常工作..

当我注释掉“创建目录”部分时,“尝试移动”部分会在控制台中记录:

  

无法移动File..Error代码4。

现在我研究了错误代码4,这意味着该目录不存在。所以我添加了代码来创建目录。

现在,当我运行程序时,会创建目录,但尝试移动部分会记录:

  

无法移动文件..错误代码512

现在研究它是由于该文件已经存在。目的地不得存在。

所以我很困惑,因为两个错误代码都在相互收缩。

{
    NSError *error;
    NSFileManager* manager = [[NSFileManager alloc] init]; 
    NSString* tempFile =[NSTemporaryDirectory() stringByAppendingPathComponent:@"recordTest.caf"];
    if (tempFile) 
    {
        // Get the Documents Directory
        NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSLog(@"Documents directory: %@",
              [manager contentsOfDirectoryAtPath:documentsDirectory error:&error]);

        //Get the User File Save Name from the text field
        NSString *UserText =   [[self FileNameText] text];

        // Rename the file, by moving the file
        NSString *filePath2 = [documentsDirectory 
                               stringByAppendingPathComponent:[NSString stringWithString:UserText]];

        // Create a Directory
        if( [manager createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,UserText]  withIntermediateDirectories:NO attributes:nil error:&error]!= YES) 
       {
           NSLog(@"Directory error");
       }

        if([manager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,UserText]])
        {
            NSLog(@"Path exist");
            NSLog(@"Documents directory: %@",
                  [manager contentsOfDirectoryAtPath:documentsDirectory error:&error]);
        }
        // Attempt the move
        if ([manager moveItemAtPath:tempFile toPath:filePath2 error:&error] != YES)
        {
            NSLog(@"Unable to move file: %@", [error localizedDescription]);
        }
        else
        {
            if ([manager removeItemAtPath:tempFile error:&error] != YES)
                NSLog(@"Unable to delete file: %@", [error localizedDescription]);
        }
    }
    [manager release];

}

2 个答案:

答案 0 :(得分:5)

问题很可能是您尝试移动的文件不存在。

替换

if (tempFile) //this will return YES every time, telling you you have a string!

 if ([manager fileExistsAtPath:tempFile]) //this will tell you if the file exists at that path

一些注释

1。要用眼睛看你正在寻找的文件是否真的存在,请检查模拟器应用程序文件夹中的类似内容:

  

/ Users / yogevshelly / Library / Application \ Support / iPhone \   模拟器/ 5.0 /应用

然后浏览到特定的应用程序

2。不要只是附加字符串来获取文件夹,执行以下操作:

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

3. 以下行将创建一个文件夹名称作为文件夹名称的文件夹!:

[manager createDirectoryAtPath:filePath2  withIntermediateDirectories:NO attributes:nil error:&error]

从文件名创建文件夹似乎很奇怪,您要做的是将文件复制到现有目录,或者首先创建一个目录并将文件复制到其中

答案 1 :(得分:0)

你使用metod

contentsOfDirectoryAtPath: withIntermediateDirectories: attributes:error

此方法创建目录但您传递文件名。 我认为你创建/ Documents / filename /而不是/ Documents / filename