#import <Foundation/Foundation.h>
NSInteger codeLineCount(NSString *path)
{
//1.获取文件管理者,这是一个单例
NSFileManager *manage = [NSFileManager defaultManager];
//2.判断文件夹or文件
BOOL dir = NO;
BOOL exist = [manage fileExistsAtPath:path isDirectory:&dir];
if (!exist) return 0;
if (dir) {
NSArray *arr = [manage contentsOfDirectoryAtPath:path error:nil];
NSInteger count = 0;
for (NSString *str in arr) {
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", path, str];
count += codeLineCount(fullPath);
}
return count;
}else{
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [content componentsSeparatedByString:@"\n"];
return arr.count;
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
#warning 这里改成你想要计算的文件夹活着文件
NSInteger count = codeLineCount(@"/Users/hj/work/hjhj");
NSLog(@"%ld",count);
}
return 0;
}