代码之家  ›  专栏  ›  技术社区  ›  dbr

在Cocoa中重命名文件?

  •  24
  • dbr  · 技术社区  · 15 年前

    如何重命名文件,使文件保持在同一目录中?

    我有一个包含文件完整路径的字符串,以及一个包含新文件名(没有路径)的字符串,例如:

    NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi";
    NSString *new_filename = @"My Correctly Named File.avi";
    

    我知道NSvileManager的 movePath:toPath:handler: 方法,但我无法训练如何构造新文件的路径。

    基本上,我在寻找与以下python代码等效的代码:

    >>> import os
    >>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi"
    >>> new_filename = "My Correctly Named File.avi"
    >>> dirname = os.path.split(old_filepath)[0]
    >>> new_filepath = os.path.join(dirname, new_filename)
    >>> print new_filepath
    /Volumes/blah/My Correctly Named File.avi
    >>> os.rename(old_filepath, new_filepath)
    
    5 回复  |  直到 11 年前
        1
  •  35
  •   Marc Charbonneau    15 年前

    nsfileManager和nsworkspace都有文件操作方法,但nsfileManager的 - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler 可能是你最好的选择。使用nsstring的路径操作方法来正确获取文件名和文件夹名。例如,

    NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
    

    这两个类在文档中都解释得很好,但是如果有什么你不理解的地方,请留下评论。

        2
  •  14
  •   Martin Gordon    15 年前

    值得注意的是,将文件移到自身会失败。我有一个方法,用下划线替换空格,将文件名改为小写,然后将文件重命名为新名称。名称中只有一个字的文件将无法重命名,因为新名称在不区分大小写的文件系统上是相同的。

    我解决这个问题的方法是进行两步重命名,首先将文件重命名为临时名称,然后将其重命名为预期名称。

    一些伪代码解释了这一点:

    NSString *source = @"/FILE.txt";
    NSString *newName = [[source lastPathComponent] lowercaseString];
    NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
    
    [[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
    

    解决方案:

    NSString *source = @"/FILE.txt";
    NSString *newName = [[source lastPathComponent] lowercaseString];
    
    NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-temp", newName]];
    NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
    
    [[NSFileManager defaultManager] movePath:source toPath:temp error:nil];
    [[NSFileManager defaultManager] movePath:temp toPath:target error:nil];
    
        3
  •  8
  •   Brock Woolf    15 年前

    我只是想让新手更容易理解。所有代码如下:

        NSString *oldPath = @"/Users/brock/Desktop/OriginalFile.png";
    NSString *newFilename = @"NewFileName.png";
    
    NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
    
    NSLog( @"File renamed to %@", newFilename );
    
        4
  •  4
  •   Matjan    11 年前

    下面是一个IOS的最新示例,nsFileManager方法有点不同:

    NSString *newFilename = [NSString stringWithFormat:@"%@.m4a", newRecording.title];
    
    NSString *newPath = [[newRecording.localPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    [[NSFileManager defaultManager] moveItemAtPath:newRecording.localPath toPath:newPath error:nil];
    
        5
  •  0
  •   Alec    11 年前

    对于最上面的结冰,NSFileManager的一个类别是:

    @implementation NSFileManager (FileManipulations)
    
    
    - (void)changeFileNamesInDirectory:(NSString *)directory changeBlock:(NSString * (^) (NSString *fileName))block
    {
        NSString *inputDirectory = directory;
    
        NSFileManager *fileManager = [NSFileManager new];
    
        NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:inputDirectory error:nil];
        for (NSString *fileName in fileNames) {
    
            NSString *newFileName =  block(fileName);
    
            NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inputDirectory, oldFileName];
            // move to temp path so case changes can happen
            NSString *tempPath = [NSString stringWithFormat:@"%@-tempName", oldPath];
            NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
    
            NSError *error = nil;
            [fileManager moveItemAtPath:oldPath toPath:tempPath error:&error];
            if (error) {
                NSLog(@"%@", [error localizedDescription]);
                return;
            }
            [fileManager moveItemAtPath:tempPath toPath:newPath error:&error];
            if (error) {
                NSLog(@"%@", [error localizedDescription]);
            }
        }
    }
    
    
    @end