i need to get all files in a directory, including all sub directories and all files in each subdirectory. in objective C, i have try to use this method
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil]
but it just give me an array of contents file in directory filePath, i cant get all files in the sub directory, can somebody help me??
thank you
Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.
Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.
Right-click that folder and select Show more options. Click Copy File List to Clipboard on the classic menu. You'll still need to paste the copied list into a text file. Launch Run, type Notepad in the Open box, and click OK.
- (void)scanPath:(NSString *) sPath {
BOOL isDir;
[[NSFileManager defaultManager] fileExistsAtPath:sPath isDirectory:&isDir];
if(isDir)
{
NSArray *contentOfDirectory=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:sPath error:NULL];
int contentcount = [contentOfDirectory count];
int i;
for(i=0;i<contentcount;i++)
{
NSString *fileName = [contentOfDirectory objectAtIndex:i];
NSString *path = [sPath stringByAppendingFormat:@"%@%@",@"/",fileName];
if([[NSFileManager defaultManager] isDeletableFileAtPath:path])
{
NSLog(path);
[self scanPath:path];
}
}
}
else
{
NSString *msg=[NSString stringWithFormat:@"%@",sPath];
NSLog(msg);
}
}
you can call this function which will log all the files in the directory, hope this will help.
You want to use enumeratorAtURL:includingPropertiesForKeys:options:error: instead. It does a deep enumeration by default:
NSURL *myDirectoryURL = [[NSBundle mainBundle] URLForResource:@"Assets" withExtension:@""];
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:myDirectoryURL includingPropertiesForKeys:[NSArray array] options:0 errorHandler:^BOOL(NSURL *url, NSError *error) {
// handle error
return NO;
}];
NSString *fileOrDirectory = nil;
while ((fileOrDirectory = [directoryEnumerator nextObject])) {
// use file or directory
}
The options argument lets you specify deep or shallow enumeration.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With