搜索文件和文件夹的驱动器

时间:2022-07-06 23:11:00

I've tried searching google, and came up with hardly anything on searching files and folders on a mac through xcode.

我试过搜索谷歌,几乎没有通过xcode搜索mac上的文件和文件夹。

is it possible and how? any code samples etc.

是可能的,怎么样?任何代码样本等

I used to programme in delphi, the snippet for searching a path is this.

我曾经在delphi中编程,搜索路径的片段就是这个。

procedure SearchFolders(path:string);
var
  sr : tsearchrec;
  res: integer;
  i:integer;
begin
  path:= includetrailingpathdelimiter(path);
  res:= findfirst(path+'*.*',faAnyfile,sr);
  while res = 0 do begin
    application.processmessages;
    if (sr.name <> '.') and (sr.name <> '..') then
      if DirectoryExists(path + sr.name) then
        SearchFolders(path + sr.name)
      else
          FileProcess.Add(path + sr.name);
          FileSize:=FileSize+sr.Size;
    res := findnext(sr);
  end;
  findclose(sr);
end;

to activate, its this SearchFolders('C:\'); and it searching the path and stores it into a stringlist.

激活它的SearchFolders('C:\');它搜索路径并将其存储到字符串列表中。

how is it done in on osx within xcode?

如何在xcode中的osx上完成?

1 个解决方案

#1


1  

Unfortunately, I don't fully understand your code. However, you'd normally use NSFileManager to interrogate the filesystem.

不幸的是,我不完全理解你的代码。但是,您通常使用NSFileManager来查询文件系统。

For instance, to list all the files (i.e. files and folders) at a specific path, you could do the following:

例如,要列出特定路径中的所有文件(即文件和文件夹),您可以执行以下操作:

- (NSArray *) listFilesAtPath:(NSString*)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL isDir;
    if(([fileManager fileExistsAtPath:path isDirectory:&isDir] == NO) && isDir) {
        // There isn't a folder specified at the path.
        return nil;
    }

    NSError *error = nil;
    NSURL *url = [NSURL fileURLWithPath:path];
    NSArray *folderItems = [fileManager contentsOfDirectoryAtURL:url
                             includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey, nil]
                                                options:NSDirectoryEnumerationSkipsHiddenFiles
                                                  error:&error];

    if (error) {
        // Handle error here
    }
    return folderItems;
}

Here's an example of how you'd use this method:

以下是您如何使用此方法的示例:

NSArray *folderItems = [self listFilesAtPath:@"/Users/1Rabbit/Desktop"];
for (NSURL *item in folderItems) {
    NSNumber *isHidden = nil;

    [item getResourceValue:&isHidden forKey:NSURLIsDirectoryKey error:nil];
    if ([isHidden boolValue]) {
        NSLog(@"%@ dir", item.path);
    }
    else {
        NSLog(@"%@", item.path);
    }
}

#1


1  

Unfortunately, I don't fully understand your code. However, you'd normally use NSFileManager to interrogate the filesystem.

不幸的是,我不完全理解你的代码。但是,您通常使用NSFileManager来查询文件系统。

For instance, to list all the files (i.e. files and folders) at a specific path, you could do the following:

例如,要列出特定路径中的所有文件(即文件和文件夹),您可以执行以下操作:

- (NSArray *) listFilesAtPath:(NSString*)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL isDir;
    if(([fileManager fileExistsAtPath:path isDirectory:&isDir] == NO) && isDir) {
        // There isn't a folder specified at the path.
        return nil;
    }

    NSError *error = nil;
    NSURL *url = [NSURL fileURLWithPath:path];
    NSArray *folderItems = [fileManager contentsOfDirectoryAtURL:url
                             includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey, nil]
                                                options:NSDirectoryEnumerationSkipsHiddenFiles
                                                  error:&error];

    if (error) {
        // Handle error here
    }
    return folderItems;
}

Here's an example of how you'd use this method:

以下是您如何使用此方法的示例:

NSArray *folderItems = [self listFilesAtPath:@"/Users/1Rabbit/Desktop"];
for (NSURL *item in folderItems) {
    NSNumber *isHidden = nil;

    [item getResourceValue:&isHidden forKey:NSURLIsDirectoryKey error:nil];
    if ([isHidden boolValue]) {
        NSLog(@"%@ dir", item.path);
    }
    else {
        NSLog(@"%@", item.path);
    }
}