1. 程式人生 > >MAC 下 用 OC 製作簡單的指令碼

MAC 下 用 OC 製作簡單的指令碼

#import <Foundation/Foundation.h>int main(int argc, constchar * argv[]) {
   
@autoreleasepool {
       
NSFileManager *fileManager = [NSFileManagerdefaultManager];
       
//在這裡獲取應用程式Documents資料夾裡的檔案及資料夾列表NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask
, YES);
       
NSString *documentDir = [documentPaths objectAtIndex:0];
       
NSError *error = nil;
       
NSArray *fileList = [[NSArrayalloc] init];
       
//fileList便是包含有該資料夾下所有檔案的檔名及資料夾名的陣列
        fileList = [fileManager
contentsOfDirectoryAtPath:documentDir error:&error];
       
//       
以下這段程式碼則可以列出給定一個資料夾裡的所有子資料夾名NSMutableArray *fileArray = [[NSMutableArrayalloc] init];
       
       
//在上面那段程式中獲得的fileList中列出資料夾名for (NSString *file in fileList) {
           
NSString *path = [documentDir stringByAppendingPathComponent:file];
           
if ([path hasSuffix:@".png"]) {
                [fileArray
addObject:path];
            }
        }
       
       
NSLog(@"Every Thing in the dir:%@",fileList);
       
NSLog(@"All folders:%@",fileArray);
       
       
for (int i = 0;  i < fileArray.count; i++) {
           
NSString *filePath = fileArray[i];
           
NSString *toPath = [NSStringstringWithFormat:@"%@/tree%d.png", documentDir,i+1];
            [fileManager
copyItemAtPath:filePath toPath:toPath error:nil];
        }
       
    }
   
return0;