• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem enumerating a directory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem enumerating a directory


  • Subject: Re: Problem enumerating a directory
  • From: Kevin Perry <email@hidden>
  • Date: Fri, 27 Jul 2012 10:10:00 -0700

You should probably try -[NSFileManager enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:]. The errorHandler block will give you more detail about any errors that occur in the middle of the enumeration (and give you the ability to ignore them).

-KP

On Jul 27, 2012, at 9:44 AM, Mark Allan <email@hidden> wrote:

> Hi all,
>
> Apologies for asking a question which has been asked many times before, but I can't seem to find an answer to this particular one.
>
> I'm trying to list a directory recursively to build up a snapshot of the contents and store them in a core data DB, but keep running into issues with the directory list.  The core data part is working fine as far as I can tell!
>
> I've tried writing a recursive method to call scandir() until the whole tree has been visited, but I'm coming unstuck converting NSStrings to const char* to char*.  Unless I supply the path directly as a hard-coded C-string "/Users/mark", it works for a while, but then sometimes it forgets to add "/Users/mark" and starts scanning directories at the root of my HD!  Hard coding the C string works perfectly but obviously isn't an option.
>
> -(void) scan: (char *)theDir{
>    struct dirent **namelist;
>    int n;
> 	size_t thisDirLength = strlen(theDir);
>
>    n = scandir(theDir, &namelist, 0, NULL);
>    if (n < 0){
>        perror("scandir");
> 	}
>    else {
>        while(n--) {
> 			theCounter++;
> 			if (theCounter >= 1000) {
> 				theCounter = 0;
> 				[[self managedObjectContext] save:NULL];
> 				[[self managedObjectContext] reset];
> 				[thePool drain];
> 				thePool = [[NSAutoreleasePool alloc] init];
> 			}
> 			if ((strcmp(namelist[n]->d_name,".") != 0) && (strcmp(namelist[n]->d_name,"..") != 0)) {
> 				char* fullPath = malloc(thisDirLength + strlen(namelist[n]->d_name) + 2);
> 				strcpy(fullPath, theDir);
> 				strcat(fullPath, "/");
> 				strcat(fullPath, namelist[n]->d_name);
>
> 				[self addEntityWithPath:[NSString stringWithCString:fullPath encoding:NSUTF8StringEncoding]];
>
> 				if (namelist[n]->d_type == DT_DIR) {
> 					[self scan:fullPath];
> 				}
> 				free(fullPath);
> 			}
>            free(namelist[n]);
>        }
>        free(namelist);
>    }
> }
>
>
> I then gave up on that approach and opted for the easier but slower cocoa solution (NSDirectoryEnumerator) but for some reason it gives up with neither an error nor a warning about half way through the tree.  Could it be that modifications to the file system during the enumeration are causing it to fail?
>
>
> -(void) startSnapshotForPath:(NSString *) thePath {
> 	int theCounter = 0;
> 	NSDirectoryEnumerator *dirEnumerator = [[NSFileManager defaultManager] enumeratorAtPath: thePath];
> 	thePool = [[NSAutoreleasePool alloc] init];
>
> 	for (NSString *theSubPath in dirEnumerator) {
> 		[self addEntityWithPath:[thePath stringByAppendingPathComponent:theSubPath]];
> 		theCounter++;
> 		if (theCounter >= 1000) {
> 			theCounter = 0;
> 			[[self managedObjectContext] save:NULL];
> 			[[self managedObjectContext] reset];
> 			[thePool drain];
> 			thePool = [[NSAutoreleasePool alloc] init];
> 		}
> 	}
>
> 	[[self managedObjectContext] save:NULL];
> 	[[self managedObjectContext] reset];
> 	[thePool drain];
> }
>
> I've also tried Uli Kusterer's UKDirectoryEnumerator but that doesn't appear to be recursive!  I suspect (although I haven't tried) that requesting the type of the path (i.e. file/directory) and creating a new UKDirectoryEnumerator for each subdirectory would be massively expensive.
>
> Does anyone have any suggestions for where I can go from here please?  How can I find out why NSDirectoryEnumerator is failing half-way through the process, and how can I stop it doing so? Failing that, does anyone have a better suggestion for how I can build the snapshot please?
>
> Many thanks
> Mark
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Problem enumerating a directory
      • From: Mark Allan <email@hidden>
References: 
 >Problem enumerating a directory (From: Mark Allan <email@hidden>)

  • Prev by Date: Problem enumerating a directory
  • Next by Date: Re: Debugging -setNeedsDisplayInRect:
  • Previous by thread: Problem enumerating a directory
  • Next by thread: Re: Problem enumerating a directory
  • Index(es):
    • Date
    • Thread