I'm trying to enumerate my entire root volume using NSDirectoryEnumerator to get an array of all of the files. While this can be done it's slow and uses a ton of memory.
Is there a better way? If so, how and I would rather have less memory usage than performance.
Thanks!
This is what I'm using:
NSFileManager *fm = [NSFileManager defaultManager];
NSMutableArray *files = [[NSMutableArray alloc] init];
NSString *dp = @"/";
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:dp];
NSString *filePath;
NSString *fullFilePath;
BOOL isDir;
filePath = [dirEnum nextObject];
while (filePath) {
@autoreleasepool
{
fullFilePath = [dp stringByAppendingPathComponent:filePath];
[fm fileExistsAtPath:fullFilePath isDirectory:&isDir];
if (isDir==false) {
[files addObject:fullFilePath];
}
filePath = [dirEnum nextObject];
}
}
I'm having similar issue too. This is using Swift these are the two I got to work. The longer one uses less memory.
let fileManager:NSFileManager = NSFileManager()
let files = fileManager.enumeratorAtPath("/")
while let file: AnyObject = files?.nextObject() {
if file.hasSuffix("pst") {
self.OutPutText.stringValue += "\(file) \n"
}
second example:
func searchPst(path: String) {
let files = NSFileManager.defaultManager();
let dir = files.contentsOfDirectoryAtPath(path, error: nil);
if dir == nil{
return
}
var isDir : ObjCBool = false
for file in dir! {
var file2 = path + (file as! String)
if path != "/"{
file2 = path + "/" + (file as! String)
}
if files.fileExistsAtPath(file2, isDirectory:&isDir)
{
if isDir{
if path == "/" {
searchPst(file2)
}
else{
if path == "/Volumes"
{
}
else
{
let file3 = path + "/" + (file as! String)
searchPst(file3)
}
}
}
}
if file.hasSuffix("pst") {
if path == "/"{
OutPutText.stringValue += path + (file as! String) + "\n"
}
else{
OutPutText.stringValue += path + "/" + (file as! String) + "\n"
}
}
}