工具:把Mac上某个目录下所有文件移动到一层目录下

Posted 想名真难

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工具:把Mac上某个目录下所有文件移动到一层目录下相关的知识,希望对你有一定的参考价值。

比如说,某个文件夹下有很多文件,并且层级很深。想要把这些文件都放到同一个文件夹下。

 变成这样

 

可以使用下面代码执行,一定要建立命令行项目。

#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

以上是关于工具:把Mac上某个目录下所有文件移动到一层目录下的主要内容,如果未能解决你的问题,请参考以下文章