Reputation: 99
I'm learning Objective-C
and I'm converting an Applescript utility to Objective-C
.
I need to understand how to get the NSFileCreationDate
of all files in the directory so I can copy the newest file to another location. I don't know how to get the NSFileCreationDate
of each file in the directory.
This code gets the listing of the directory but each time I try to get the NSFileCreationDate
it gives me the NSFileCreationDate
of the enclosing folder.
I have to read a plist file to get the path to the backup location.
Copy the newest file to * wholepath , file on desktop
NSString filePath = [@"~/Library/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];
I've googled this info and since I'm new to this the answers I'm finding are not making a lot of sense to me. If anyone knows of a better way I'm all ears.
Thanks All !!
Upvotes: 1
Views: 1169
Reputation: 99
I figured it out with the help of you guys leading me in the right direction. Thanks to everyone who commented!
Here my final code to help anyone else with a similar issue.
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!");
}
Upvotes: 0
Reputation: 1275
The following may help
// 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;
}
You can omit the unHideDir
and ((showHiddenFiles | unHideDir| unHideAllDir)
This relies on an array of properties
. I set this in 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:)]];
}
}
You can unpack the dates with something like
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];
Upvotes: 0
Reputation: 1478
This should help:
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);
}
Upvotes: 2