输出超长字符串
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String] );
比如说,某个文件夹下有很多文件,并且层级很深。想要把这些文件都放到同一个文件夹下。
变成这样
可以使用下面代码执行,一定要建立命令行项目。
#import "FileToOnePath.h"
/// 要处理的文件夹路径
static NSString *const FileToOnePathDirectoryPath = @"/Users/admin/Downloads/ULSetting";
@implementation FileToOnePath
+ (void)load {
[self fileToOnePath];
}
// 文件挪到一个目录下
+ (void)fileToOnePath {
NSString *filePath = FileToOnePathDirectoryPath;
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (!fileExist) {
NSLog(@"文件不存在,结束");
}
NSArray *array = [[NSFileManager defaultManager] subpathsAtPath:filePath];
NSLog(@"%@",array);
for (NSString *path in array) {
NSString *fullPath = [filePath stringByAppendingPathComponent:path];
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir];
/// 是文件夹, 不用处理,后面统一删除
if (isDir) {
continue;
}
// 是文件
NSError *error = nil;
NSString *toPath = [filePath stringByAppendingPathComponent:path.lastPathComponent];
[[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toPath error:&error];
if (error) {
NSLog(@"出错了 %@ ",error);
} else {
NSLog(@"挪动成功 %@ %@",fullPath,toPath);
}
}
for (NSString *path in array) {
NSString *fullPath = [filePath stringByAppendingPathComponent:path];
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir];
/// 是文件夹, 不用处理,后面统一删除
if (isDir) {
[[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
}
}
}
@end
查找工程下的所有log所在文件和log内容。
1.查找对应目录下所有的文件
2.读取文件内容
3.取出每一行信息
4.判断每行信息是否包含log
#import "FindAllChineseLog.h"
#import "Header.h"
/// 要处理的文件夹路径
static NSString *const FindAllChineseLogDirectoryPath = @"工程路径";
@interface FindAllChineseLog ()
@property (nonatomic, assign) NSInteger lineNum;
@property (nonatomic, strong) NSArray *logStrArray;
@end
@implementation FindAllChineseLog
+ (void)load {
FindAllChineseLog *tool = [[FindAllChineseLog alloc] init];
[tool findAllChineseLog];
}
// 文件挪到一个目录下
- (void)findAllChineseLog {
NSString *filePath = FindAllChineseLogDirectoryPath;
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (!fileExist) {
NSLog(@"文件不存在,结束");
}
NSArray *array = [[NSFileManager defaultManager] subpathsAtPath:filePath];
for (NSString *path in array) {
NSString *fullPath = [filePath stringByAppendingPathComponent:path];
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir];
/// 是文件夹, 不用处理,后面统一删除
if (isDir) {
continue;
}
// 是.m文件, 其他文件不可能存在中文日志
if ([path.lastPathComponent containsString:@".m"]) {
[self handleFile:fullPath];
}
}
}
- (void)handleFile:(NSString *)filePath {
@autoreleasepool {
NSError * error = nil;
NSString *str = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"出错了, %@ %@",filePath,error);
}
NSArray *array = [str componentsSeparatedByString:@"\n"];
[array enumerateObjectsUsingBlock:^(NSString * lineStr, NSUInteger idx, BOOL * _Nonnull stop) {
lineStr = [lineStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([self checkLineStr:lineStr]) {
NSLog(@"%zd %@ %@",self.lineNum,filePath,lineStr);
self.lineNum ++;
}
}];
}
}
/// 1.包含log, 2.没有被注释
- (BOOL)checkLineStr:(NSString *)lineStr {
if ([lineStr containsString:@"Log"]) {
for (NSString *logStr in self.logStrArray) {
// 包含log
if ([lineStr containsString:logStr]) {
// 2.没有被注释
if ([lineStr hasPrefix:@"//"] == NO) {
return YES;
}
}
}
}
return NO;
}
- (NSArray *)logStrArray {
if (_logStrArray == nil) {
_logStrArray =@[
@"NSLog",
@"DDLog"
];
}
return _logStrArray;
}
@end
GitHub - guochaoshun/GCS_OC_StringTool
精彩文章
发表评论