Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

List Files in SubFolder (only in SubFolders )from a Folder

I need get list files in subFolder from a Folder main.

This is my code, but the value "filelist1" always show me empty array.


//

// ViewController.m

// MoveFiles

//

// Created by Jose Antonio Vazquez Mingorance on 10/10/15.

// Copyright (c) 2015 Jose Vazquez. All rights reserved.

//


#import "ViewController.h"


@implementation ViewController


- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

}


- (void)setRepresentedObject:(id)representedObject {

[super setRepresentedObject:representedObject];

// Update the view, if already loaded.

}


// Solo los directorios


- (BOOL)onlyFolders:(int)i filelist:(NSArray *)filelist url:(NSURL *)url filemgr:(NSFileManager *)filemgr {

NSString *path = [[url path] stringByAppendingPathComponent:[filelist objectAtIndex: i]];

BOOL isDir = NO;

[filemgr fileExistsAtPath:path isDirectory:(&isDir)];

return isDir;

}



// Listado de archivos



// Abre panel de seleccion de carpeta

- (IBAction)AbreDirectorio:(id)sender {

NSOpenPanel *panel = [NSOpenPanelopenPanel];

[panel setCanChooseFiles:NO];

[panel setCanChooseDirectories:YES];

[panel setAllowsMultipleSelection:YES]; // yes if more than one dir is allowed

NSInteger clicked = [panel runModal];

if (clicked == NSFileHandlingPanelOKButton) {

for (NSURL *url in [panel URLs]) {

NSFileManager *filemgr;

NSArray *filelist;

long count;

int i;

NSString *subDir;

NSArray *filelist1;

NSMutableString *urlSub = [[NSMutableStringalloc]init];

NSString *enlace = @"/";

NSString *subDir1;

// NSString *immutableString ;

long count1;

long j;

filemgr = [NSFileManager defaultManager];

filelist = [filemgr contentsOfDirectoryAtPath: [url path] error: nil];

count = [filelist count];

// Todos los archivos

for (i = 0; i < count; i++)

{

BOOL isDir;

isDir = [self onlyFolders:i filelist:filelist url:url filemgr:filemgr];

if (isDir){

// Subdirectorios

//NSLog(@"%@", url);

// NSLog (@"%@", [filelist objectAtIndex: i]);

NSString *temp1 = [url absoluteString];

NSString *temp2 = (NSString *)[filelist objectAtIndex: i] ;

subDir = [temp1 stringByAppendingString:temp2];

subDir1 = [subDir stringByAppendingString:enlace];


NSLog(@"%@", urlSub);

// filelist1 show always empy

filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:subDir1 error:nil];

count1 = [filelist1 count];

for (j = 0; j < count; j++)

{

NSLog (@"%@", [filelist1 objectAtIndex: j]);

}

}

}

}

NSLog(@"Fin");

}

}

@end

iMac, OS X Yosemite (10.10.5), Is a general application

Posted on Oct 12, 2015 11:57 AM

Reply
Question marked as Best reply

Posted on Oct 12, 2015 11:52 PM

My guess is you're mixing URLs with paths. For example, "temp1" in the statement


NSString *temp1 = [url absoluteString];


probably contains a URL scheme such as "file:///"


and you're passing this string into a method that takes a path and not a URL - contentsOfDirectoryAtPath


So, my advice to you would be to construct a file path and not a URL, for example


NSString *subDir1 = "/User/Documents" + "/" + [filelist objectAtIndex: i]


mingorance - Does your value for "subDir1" begin with a URL scheme? If so use [url path] instead of [url absoluteString]

4 replies
Question marked as Best reply

Oct 12, 2015 11:52 PM in response to mingorance

My guess is you're mixing URLs with paths. For example, "temp1" in the statement


NSString *temp1 = [url absoluteString];


probably contains a URL scheme such as "file:///"


and you're passing this string into a method that takes a path and not a URL - contentsOfDirectoryAtPath


So, my advice to you would be to construct a file path and not a URL, for example


NSString *subDir1 = "/User/Documents" + "/" + [filelist objectAtIndex: i]


mingorance - Does your value for "subDir1" begin with a URL scheme? If so use [url path] instead of [url absoluteString]

Oct 12, 2015 11:56 PM in response to disguise

Hello.

First to all, thank for you help.


I tried all options, "url path" and "url absoluteString", but allways get me a empty array.


I have tried:

filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[subDir path] error:nil];

filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[subDir1 path] error:nil];


I don't know where is my error.

(I am beginner in this programming languaje)


Thank again.

Oct 13, 2015 7:56 AM in response to mingorance

Using the exact same code you posted in your original post...


First, just prior to the comment

// filelist1 show always empy


insert this line exactly and post the results

NSLog(@"subDir1 = \"%@\"", subDir1);


Second, the method "contentsOfDirectoryAtPath" takes a pointer to an NSError object - use it to debug


NSError *error = nil;

filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:subDir1 error:&error];


if (filelist1 == nil) {

NSLog(@"contentsOfDirectoryAtPath FAILED, error = (%d, %@)", [error code], [error localizedDescription]);

} else {

NSLog(@"contentsOfDirectoryAtPath SUCCEEDED");

}


Third, a debugging technique to use when having difficulty with a method is to hardcode arguments to isolate the issue. So, if you can't get something to work state explicitly the arguments. For example, use the following line (substituting a value directory path from your system).


filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/User/your_user_name/Junk" error:&error];


Fourth, add NSLog statements to display results.


Hope these debugging techniques help -

Oct 14, 2015 10:38 AM in response to disguise

I have found the problem:


// filelist1 show alwais empy

NSError *error = nil; //new

// NSString *hardFile = (@"//Users/joseantoniovazquez/Downloads/Borrar/cienciaes/");


filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:subDir error:&error];

// filelist1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hardFile error:&error];

if (filelist1 == nil) {

NSLog(@"contentsOfDirectoryAtPath FAILED, error = (%d, %@)", [error code], [error localizedDescription]);

} else {

NSLog(@"contentsOfDirectoryAtPath SUCCEEDED");

}


2015-10-14 19:35:41.497 MoveFiles[862:193016] subDir1 = "file:///Users/joseantoniovazquez/Downloads/Borrar/cienciaes/"

2015-10-14 19:35:49.298 MoveFiles[862:193016] contentsOfDirectoryAtPath FAILED, error = (260, The folder “cienciaes” doesn’t exist.)

(lldb)

I see the problem in "File:///"

But if i put directly:

NSString *hardFile = (@"//Users/joseantoniovazquez/Downloads/Borrar/cienciaes/");

Work fine:

I try now to resolv this problem....

List Files in SubFolder (only in SubFolders )from a Folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.