Linux world
Linux world

Reputation: 3768

how to create a directory/folder in app with current date as a name

i want to create a folder in our app local directories ...

the file structure i want to create will look like current date year/month/date the root directory of this structure is year inside that current month inside that current date folders ..

this way i hav to create the folders...

any help appreciated...

Upvotes: 0

Views: 612

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

this is a straight forward task.
First you have to split the date into components.

NSDate *date = [NSDate date];
NSUInteger dateFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:dateFlags fromDate:date];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];

Then create the path you want to create.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dir = [NSString stringWithFormat:@"%d/%d/%d", year, month, day];
NSString *path = [documentsDirectory stringByAppendingPathComponent:dir];

and finally, create the directory

NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:0 error:&error])
    NSLog(@"Error creating path %@ [%@]", path, error);

Upvotes: 1

Related Questions