Reputation: 55
I've tried searching google, and came up with hardly anything on searching files and folders on a mac through xcode.
is it possible and how? any code samples etc.
I used to programme in delphi, the snippet for searching a path is this.
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.
how is it done in on osx within xcode?
Upvotes: 1
Views: 339
Reputation: 1542
Unfortunately, I don't fully understand your code. However, you'd normally use NSFileManager
to interrogate the filesystem.
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);
}
}
Upvotes: 1