2014-01-06 71 views
1

我在學習Objective-C,並且正在將Applescript實用程序轉換爲Objective-C如何獲取目標文件的創建日期目標c

我需要了解如何獲取目錄中所有文件的NSFileCreationDate,以便我可以將最新的文件複製到其他位置。我不知道如何獲取目錄中每個文件的NSFileCreationDate

此代碼獲取目錄的列表,但每次我嘗試獲取NSFileCreationDate時,它都給出了包含文件夾的NSFileCreationDate

我必須閱讀plist文件以獲取備份位置的路徑。對於備份位置

  1. 讀取plist文件
  2. 獲取創建文件的日期
  3. 複製的最新文件* wholepath,在桌面上的文件

    的NSString 文件路徑= [@「〜/資源庫/ Preferences/com.viive.Viive.plist「stringByExpandingTildeInPath」; NSMutableDictionary plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

    NSString *value; 
    value = [plistDict objectForKey:@"backupPath"]; 
    
    
    NSFileManager *filemgr; 
    NSArray *filelist; 
    filemgr = [NSFileManager defaultManager]; 
    filelist = [filemgr contentsOfDirectoryAtPath:value error: nil]; 
    NSArray *array = filelist; 
    NSUInteger index = 0; 
    for (id element in array) { 
    NSLog(@"Element at index %lu is: %@", (unsigned long)index, element); 
    index++; 
    } 
    NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:////  error:nil]; 
    NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate 
    NSLog(@"%@",result); 
    
    ////// 
    
    NSString* desktop = [@"~/Desktop/LOGS-I-NEED-" stringByExpandingTildeInPath]; 
        NSString* computername = [[NSHost currentHost] localizedName]; 
        NSString* wholepath = [desktop stringByAppendingString:computername]; 
    

我GOOGLE了這個信息,因爲我是新來的這個我發現不作出了很多的意義,我的答案。如果有人知道更好的方式,我全都耳熟能詳。

謝謝大家!

回答

2

這應有助於:

for (NSString file in array) { 
    NSLog (@"%@", file); 
    NSString *path = [filePath stringByAppendingPathComponent: file]; 
    NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil]; 
    NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate 
    NSLog(@"%@",result); 
} 
+0

此代碼給我 - [__ NSCFString路徑]:無法識別的選擇發送到實例0x106107c30 – Breathable

+0

出現在代碼中的錯誤。請再試一次。 – Max

+0

我得到(空)然後,如果我再試一次我得到的文件內容的列表,但然後我得到這個*** - [__ NSArrayM objectAtIndex:]:索引34超越邊界[0 .. 33] – Breathable

0

以下信息可能有助於

// This is the main routine to read the contents of a directory (into array of URL) 
- (NSArray *)readDirectory:(NSString *)path error:(NSError **)error { 
    NSURL *url = [NSURL fileURLWithPath:path isDirectory:YES]; 
    NSArray *array = [[NSFileManager new] 
         contentsOfDirectoryAtURL:url 
         includingPropertiesForKeys:properties 
         options:(NSDirectoryEnumerationSkipsPackageDescendants | 
           ((showHiddenFiles | unHideDir| unHideAllDir) ? 0 : NSDirectoryEnumerationSkipsHiddenFiles)) 
         error:error]; 
    if (unHideDir) { 
     NSIndexSet *unDotted = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger index, BOOL *stop) { 
      NSString *name; 
      [obj getResourceValue:&name forKey:NSURLNameKey error:nil]; 
      if ([name characterAtIndex:0] == '.') return NO; // exclude .files 
      return YES; 
     }]; 
     return [array objectsAtIndexes:unDotted]; 
    } 
    return array; 
} 

可以省略unHideDir((showHiddenFiles | unHideDir| unHideAllDir)

這依賴的properties陣列上。我設置在initialize

+ (void)initialize { 
    if (self == [DirectoryItem class]) { 
     leafNode = [[NSMutableArray alloc] init]; 
     [self loadPreferences]; 
     properties = [NSArray arrayWithObjects: 
         NSURLNameKey, 
         NSURLFileSizeKey, NSURLIsAliasFileKey, NSURLIsPackageKey, 
         NSURLIsDirectoryKey, NSURLIsSymbolicLinkKey, NSURLIsRegularFileKey, 
         NSURLCreationDateKey, NSURLContentModificationDateKey, 
         NSURLLocalizedTypeDescriptionKey, nil]; 
     dirSortDescriptor = [NSArray arrayWithObject: 
          [[NSSortDescriptor alloc] initWithKey:COLUMNID_NAME 
                 ascending:YES 
                  selector:@selector(localizedStandardCompare:)]]; 
    } 
} 

你可以用一些解開日期像

NSDate *tempDate; 
[path getResourceValue:&tempDate forKey:NSURLCreationDateKey error:nil]; 
if ([tempDate timeIntervalSince1970] < 24*3600) tempDate = nil; 
cDate = [tempDate copy]; 
[path getResourceValue:&tempDate forKey:NSURLContentModificationDateKey error:nil]; 
wDate = [tempDate copy]; 
0

我想通了跟你們領導讓我在正確的方向的幫助。 感謝大家評論!

在這裏我的最終代碼來幫助任何其他人有類似的問題。

NSString *s = [NSString stringWithFormat:@"tell application \"12345\" to backup"]; 
NSAppleScript *as = [[NSAppleScript alloc] initWithSource: s]; 
[as executeAndReturnError:nil]; 

/////// 

NSString *filePath = [@"~/Library/Preferences/com.12345.12345.plist" stringByExpandingTildeInPath]; 
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

NSString *value; 
value = [plistDict objectForKey:@"backupPath"]; 

///// 

NSDateFormatter *dateformat = [[NSDateFormatter alloc] init]; 
[dateformat setDateFormat:@"YYY-MM-dd'T'HH:MM:ss'"]; 
NSDate *maxdate = [NSString stringWithFormat:@"1900-01-01 00:00:00 +0000"]; 

NSFileManager *filemgr; 
NSArray *filelist; 

filemgr = [NSFileManager defaultManager]; 
filelist = [filemgr contentsOfDirectoryAtPath:value error: nil]; 
NSArray *array = filelist; 
NSUInteger index = 0; 
NSString *currentdb; 
for (id element in array) { 
    NSLog(@"Element at index %lu is: %@", (unsigned long)index, element); 

    NSString* slash = @"/"; 
    NSString* elementwithpath = value; 
    NSString* wholepathh = [elementwithpath stringByAppendingString:slash]; 
    wholepathh = [wholepathh stringByAppendingString:element]; 


    NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:wholepathh error:nil]; 
    NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate 
    NSLog(@"%@",result); 
    index++; 

    if (result > maxdate) 
    { 
     maxdate = result; 
     currentdb = wholepathh; 
    } 
    NSLog(wholepathh); 
} 
NSLog(currentdb); 

///// 

NSString* desktop = [@"~/Desktop/LOGS-I-NEED-" stringByExpandingTildeInPath]; 
NSString* computername = [[NSHost currentHost] localizedName]; 
NSString* wholepath = [desktop stringByAppendingString:computername]; 
NSString* slash = @"/"; 

NSString* filename = [[currentdb lastPathComponent] stringByDeletingPathExtension]; 


NSString* totalpath =[ wholepath stringByAppendingString:slash]; 
NSString* finalpath =[ totalpath stringByAppendingString:filename]; 

NSError * error = nil; 
filemgr = [NSFileManager defaultManager]; 


([filemgr copyItemAtPath:currentdb toPath:finalpath error:&error]); 

if (error != nil) 
{ 
    NSLog(@"error creating directory: %@", error); 
    //.. 
} 
else 
{ 
    NSLog(@"Copying the 12345 Backup worked!"); 
}