#define NSLog(FORMAT, …) fprintf(stderr,”%sn”,[[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
2.读取文件内容
#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
原文地址:https://blog.csdn.net/u014600626/article/details/128727086
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_22482.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。