Created
June 16, 2013 07:11
-
-
Save katsuhide/5791224 to your computer and use it in GitHub Desktop.
FileManager(ファイル操作)周り
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ファイルマネージャを作成 | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
// ファイルが存在するか? | |
if ([fileManager fileExistsAtPath:filePath]) { // yes | |
NSLog(@"%@は既に存在しています", filePath); | |
} else { | |
NSLog(@"%@は存在していません", filePath); | |
} | |
// NSURL -> ファイルパス | |
NSURL *url | |
[url path] | |
// ディレクトリを作成 | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSError *error = nil; | |
NSString *workPath = @"file_path"; | |
if(![fileManager createDirectoryAtPath:workPath withIntermediateDirectories:YES attributes:nil error:&error]){ | |
NSLog(@"Couldn't create the data store directory.[%@, %@]", error, [error userInfo]); | |
return; | |
} | |
// 対象のパスのファイル一覧を取得 | |
NSString *directoryPath = @"hoge"; | |
NSFileManager *fileManager=[[NSFileManager alloc] init]; | |
NSError *error = nil; | |
NSArray *allFileName = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error]; | |
if (error) return nil; | |
// 拡張子で絞り込む | |
NSMutableArray *filePathList = [[NSMutableArray alloc] init]; | |
// 拡張子条件が存在する場合、各ファイルの拡張子が一致するかを確認したうえでフルパスのFileListを生成 | |
for (NSString *fileName in allFileName) { | |
if([extension length] == 0){ | |
// 拡張子条件が空の場合 | |
NSString *fullPath = [directoryPath stringByAppendingPathComponent:fileName]; | |
[filePathList addObject:fullPath]; | |
}else if ([[fileName pathExtension] isEqualToString:extension]) { | |
// 拡張子条件が存在する場合 | |
NSString *fullPath = [directoryPath stringByAppendingPathComponent:fileName]; | |
[filePathList addObject:fullPath]; | |
} | |
} | |
// サブディレクトリも検索する場合 | |
NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directoryPath]; | |
for(NSString *file in directoryEnumerator){ | |
NSLog(@"deep:%@", file); | |
} | |
// ファイルのコピー | |
BOOL 変数 = [《NSFileManager*》 copyItemAtPath:《NSString*》toPath:《NSString*》 error:《NSError**》]; | |
// フィアルの移動 | |
BOOL 変数 = [《NSFileManager*》 moveItemAtPath:《NSString*》toPath:《NSString*》 error:《NSError**》]; | |
// ファイルの削除 | |
BOOL 変数 = [《NSFileManager*》 removeItemAtPath:《NSString*》error:《NSError**》]; | |
// ファイルの存在チェック | |
BOOL 変数 = [《NSFileManager*》 fileExistsAtPath:《NSString*》]; | |
// ディレクトリかどうかのチェック | |
BOOL 変数 = [《NSFileManager*》 fileExistsAtPath:《NSString*》 isDirectory:《BOOL*》]; | |
// テキストファイルの読み込み | |
NSString *変数 = [NSString stringWithContentsOfFile:《NSString*》 encoding:《NSStringEncoding》 error:《NSError**》]; | |
// デキストファイルの保存 | |
BOOL 変数 = [《NSString*》 writeToFile:《NSString*》atomically:《BOOL》 encoding:《NSStringEncoding*》 error:《NSError**》]; | |
// Homeディレクトリのパスを取得 | |
NSString* 変数 = NSHomeDirectory(); | |
// チルダ付きパスをフルパスに変換 | |
NSString *変数 = [《NSString*》 stringByExpandingTildeInPath]; | |
// フルパスをチルダ付きパスに変換 | |
NSString *変数 = [《NSString*》 stringByAbbreviatingWithTildeInPath]; | |
// パスの末尾に指定の拡張子を追加 | |
NSString *変数 = [《NSString*》 stringByAppendingPathExtension:《NSString*》]; | |
// ファイルパスから拡張子を取り除く | |
NSString *変数 = [《NSString*》 stringByDeletingPathExtension]; | |
// 引数のテキストをパスに追加します | |
NSString *変数 = [《NSString*》 stringsByAppendingPaths:《NSArray*》]; | |
// 最後のパスの要素を削除したものを返します | |
NSString *変数 = [《NSString*》 stringByDeletingLastPathComponent]; | |
// パスは、ルートからの階層をテキストとしてまとめたものです。テキストとしては1つですが、1つ1つの階層のデータが集まったものと考えることもできますね。こうした1つ1つの要素の構成としてパスを扱うこともできます。 | |
NSArray *変数 = [《NSString*》pathComponents]; | |
// その逆 | |
NSString *変数 = [NSString pathWithComponents:《NSArray*》]; | |
// ファイルパスの連結 | |
// レシーバの末尾か引数の先頭にディレクトリ階層を表すセパレーター「/(スラッシュ)」が抜けていたり、逆に二重に記述してしまっていても自動的に補完して結合する | |
NSString *path = - (NSString *)stringByAppendingPathComponent:(NSString *)aString | |
// ファイルの拡張子を取得 | |
NSString* path = @"/path/to/file.txt"; | |
[path pathExtension]; // txt | |
// ファイル名の取得 | |
[path lastPathComponent]; // file.txt | |
// Application内部のファイルのパスを取得 | |
NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"]; | |
// フィアルのタイムスタンプを取得 | |
-(NSDate*)getFileTimeStamp:(NSString*)fileName andDirectoryPath:(NSString*)directoryPath{ | |
NSError *error = nil; | |
NSString *filePath = [NSString stringWithString:[directoryPath stringByAppendingPathComponent:fileName]]; | |
NSDictionary* dicFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]; | |
return [dicFileAttributes objectForKey:@"NSFileModificationDate"]; | |
} | |
// ファイルのタイムスタンプでソート | |
NSMutableArray *attributes = [NSMutableArray array]; | |
NSString *directoryPath = @"/Users/AirMyac/Library/Caches/Metadata/Safari/History"; | |
NSString *extension = @"webhistory"; | |
NSError *error = nil; | |
NSArray *allFileList = [self getFileList:directoryPath andFileExtension:extension andIncludeSubDirectory:NO]; | |
for(NSString *fileName in allFileList){ | |
NSMutableDictionary *tmpDictionary = [NSMutableDictionary dictionary]; | |
NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName]; | |
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]; | |
[tmpDictionary setDictionary:attr]; | |
[tmpDictionary setObject:filePath forKey:@"FilePath"]; | |
[attributes addObject:tmpDictionary]; | |
} | |
// ソート | |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:NSFileCreationDate ascending:NO]; | |
NSArray *sortarray = [NSArray arrayWithObject:sortDescriptor]; | |
// 並び替えられたファイル配列 | |
NSArray *resultarray = [attributes sortedArrayUsingDescriptors:sortarray]; | |
/* | |
* 指定したファイルと指定した時間の比較を実施 | |
*/ | |
-(NSComparisonResult)compareFileTimeStamp:(NSDate*)lastExecutedTime andFilePath:(NSString*)filePath{ | |
// ファイルのタイムスタンプを取得 | |
NSError *error = nil; | |
NSDictionary* dicFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]; | |
if (error) { | |
return -1; | |
} | |
// 比較 | |
NSDate *fileTimeStamp = [dicFileAttributes objectForKey:@"NSFileModificationDate"]; | |
#if DEBUG | |
NSLog(@"file:%@, target:%@", [fileTimeStamp toLocalTime], [lastExecutedTime toLocalTime]); | |
#endif | |
NSComparisonResult result = [fileTimeStamp compare:lastExecutedTime]; | |
return result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment