开发者

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 开发者_Python百科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...


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜