ココアでファイルの名前を変更?
-
22-08-2019 - |
質問
どのように私は同じディレクトリにあるファイルを保ち、ファイルの名前を変更するのでしょうか?
私は、ファイルへの完全なパスを含む文字列、および新しいファイル名(およびパスなし)を含む文字列を持っている、例えばます:
NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi";
NSString *new_filename = @"My Correctly Named File.avi";
私は、NSFileManagerの<のhref = "http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/について知っていますmovePath:toPath:ハンドラ:」のrel = "noreferrer"> movePath:toPath:ハンドラ:の方法が、新しいファイルのパスを構築する方法を、私はできないワークアウト..
基本的に私は、次の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)
解決
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段階の名前の変更を行うことでした。
いくつかの擬似コードは、このことを説明します
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];
私は初心者のために理解しこれを容易にしたかったです。ここでは、すべてのコードがあります:
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 );
ここで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];
トップの上のアイシングについては、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