LoginSignup
22
22

More than 5 years have passed since last update.

ファイルの時間とサイズを取得する

Posted at

ファイル属性はNSFileManagerクラスを使えば簡単に取得できます。
参考でC言語のstat(2)を使う方法も載せておきます。詳細はstat(2)のmanpageを参照してください。

NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"first" ofType:@"png"];

// NSFileManagerを使ってファイルの属性を取得する
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attribute = [fm attributesOfItemAtPath:path error:nil];
NSDate *creationDate = [attribute objectForKey:NSFileCreationDate];
NSDate *modificationDate = [attribute objectForKey:NSFileModificationDate];
NSNumber *fileSize = [attribute objectForKey:NSFileSize];

// stat(2)を使ってファイルの属性を取得する
// #include <sys/stat.h>
// #include <time.h>
struct stat sb;
memset(&sb, 0x00, sizeof(struct stat));
stat(path.UTF8String, &sb);
struct tm ctime;
memcpy(&ctime, localtime(&sb.st_ctimespec.tv_sec), sizeof(struct tm));
struct tm mtime;
memcpy(&mtime, localtime(&sb.st_mtimespec.tv_sec), sizeof(struct tm));
off_t size = sb.st_size;
22
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22