unsigned long codeLineCount(NSString *Path)
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL dir = NO;
BOOL exists = [mgr fileExistsAtPath:Path isDirectory:&dir];
if (!exists) return 0;
if (dir) {
NSLog(@"是个文件夹");
NSArray *array = [mgr contentsOfDirectoryAtPath:Path error:nil];
int count = 0;
for (NSString *fileName in array) {
NSString *fullPath = [NSString stringWithFormat:@"%@/%@",Path,fileName];
count += codeLineCount(fullPath);
}
return count;
}else
{
NSString *content = [NSString stringWithContentsOfFile:Path encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [content componentsSeparatedByString:@"\n"];
return array.count;
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *filePath = @"/Users/chengyi/Desktop/未命名文件夹/cc.m";
unsigned long count = codeLineCount(filePath);
NSLog(@"%ld",count);
}
return 0;
}