Iterating through files in a folder with nested folders - Cocoa - macos

I need to access every file in a folder, including file that exist within nested folders. An example folder might look like this.
animals/
-k.txt
-d.jpg
cat/
-r.txt
-z.jpg
tiger/
-a.jpg
-p.pdf
dog/
-n.txt
-f.jpg
-p.pdf
Say that I wanted to run a process on every file within "animals" that isn't folder. What would be the best way to iterate through the folder "animals" and all of its subfolders to access every file?
Thanks.

Use NSDirectoryEnumerator to recursively enumerate files and directories under the directory you want, and ask it to tell you whether it is a file or directory. The following is based on the example listed at the documentation for -[NSFileManager enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:]:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *directoryURL = … // URL pointing to the directory you want to browse
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtURL:directoryURL
includingPropertiesForKeys:keys
options:0
errorHandler:^BOOL(NSURL *url, NSError *error) {
// Handle the error.
// Return YES if the enumeration should continue after the error.
return YES;
}];
for (NSURL *url in enumerator) {
NSError *error;
NSNumber *isDirectory = nil;
if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
// handle error
}
else if (! [isDirectory boolValue]) {
// No error and it’s not a directory; do something with the file
}
}

Maybe you can use something like this:
+(void)openEachFileAt:(NSString*)path
{
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
for (NSString * file in enumerator)
{
// check if it's a directory
BOOL isDirectory = NO;
NSString* fullPath = [path stringByAppendingPathComponent:file];
[[NSFileManager defaultManager] fileExistsAtPath:fullPath
isDirectory: &isDirectory];
if (!isDirectory)
{
// open your file (fullPath)…
}
else
{
[self openEachFileAt: fullPath];
}
}
}

Here is a swift version:
func openEachFile(inDirectory path: String) {
let subs = try! FileManager.default.subpathsOfDirectory(atPath: path)
let totalFiles = subs.count
print(totalFiles)
for sub in subs {
if sub.hasSuffix(".DS_Store") {
//a DS_Store file
}
else if sub.hasSuffix(".xcassets") {
//a xcassets file
}
else if (sub as NSString).substring(to: 4) == ".git" {
//a git file
}
else if sub.hasSuffix(".swift") {
//a swift file
}
else if sub.hasSuffix(".m") {
//a objc file
}
else if sub.hasSuffix(".h") {
//a header file
}
else {
// some other file
}
let fullPath = (path as NSString).appendingPathComponent(sub)
}
}

Here's a solution using -subpathsOfDirectoryAtPath:rootPath, with file URLs and modern Objective-C nullability bells & whistles.
typedef void (^FileEnumerationBlock)(NSURL *_Nonnull fileURL);
#interface NSFileManager (Extensions)
- (void)enumerateWithRootDirectoryURL:(nonnull NSURL *)rootURL
fileHandler:(FileEnumerationBlock _Nonnull)fileHandler
error:(NSError *_Nullable *_Nullable)error;
#end
#implementation NSFileManager (Extensions)
- (void)enumerateWithRootDirectoryURL:(NSURL *)rootURL
fileHandler:(FileEnumerationBlock)fileHandler
error:(NSError **)error {
NSString *rootPath = rootURL.path;
NSAssert(rootPath != nil, #"Invalid root URL %# (nil path)", rootURL);
NSArray *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:rootPath
error:error];
if (!subs) {
return;
}
for (NSString *sub in subs) {
fileHandler([rootURL URLByAppendingPathComponent:sub]);
}
}
#end
… and the same in Swift:
func enumerate(rootDirectoryURL rootURL: NSURL, fileHandler:(URL:NSURL)->Void) throws {
guard let rootPath = rootURL.path else {
preconditionFailure("Invalid root URL: \(rootURL)")
}
let subs = try NSFileManager.defaultManager().subpathsOfDirectoryAtPath(rootPath)
for sub in subs {
fileHandler(URL: rootURL.URLByAppendingPathComponent(sub))
}
}

This code worked for me.
NSMutableString *allHash;
-(NSString*)getIterate:(NSString*)path {
allHash = [NSMutableString stringWithString:#""];
NSDirectoryEnumerator *de= [[NSFileManager defaultManager] enumeratorAtPath:path];
NSString *file;
BOOL isDirectory;
for(file in de)
{
//first check if it's a file
NSString* fullPath = [NSString stringWithFormat:#"%#/%#",path,file];
BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
NSLog(#"Check=>%d",fileExistsAtPath);
if (!isDirectory) //its a file
{
//Do with filepath
}
else{ //it's a folder, so recurse
[self enumerateFolder:fullPath];
}
}
return allHash;
}
-(void) enumerateFolder:(NSString*)fileName
{
NSDirectoryEnumerator *de= [[NSFileManager defaultManager] enumeratorAtPath:fileName];
NSString* file;
BOOL isDirectory;
for(file in de)
{
//first check if it's a file
BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory];
if (fileExistsAtPath) {
if (!isDirectory) //its a file
{
//Do with file
}
else{ //it's a folder, so recurse
[self enumerateFolder:file];
}
}
else printf("\nenumeratefolder No file at path %s",[file UTF8String]);
}
}

In Swift we can try the following.
let docsDir = NSHomeDirectory().appending("/Documents")
let localFileManager = FileManager()
let dirEnum = localFileManager.enumerator(atPath: docsDir)
while let file = dirEnum?.nextObject() as? String {
if file.hasSuffix(".doc") {
print(docsDir.appending("/\(file)"))
}
}
Reference
https://developer.apple.com/documentation/foundation/filemanager/1408726-enumerator

Related

Validating URL from drag and drop in a sandbox

With file access in a sandboxed osx app with swift in mind, does it work the same with URLs provided via Finder or other apps drops?
As there's no NSOpenPanel call to afford folder access as in this example, just urls - I think the folder access is implicit since the user dragged the file from the source / desktop "folder" much the same as implicit selection via the open dialog.
I have not begun the sandbox migration yet but wanted to verify my thinking was accurate, but here's a candidate routine that does not work in sandbox mode:
func performDragOperation(_ sender: NSDraggingInfo!) -> Bool {
let pboard = sender.draggingPasteboard()
let items = pboard.pasteboardItems
if (pboard.types?.contains(NSURLPboardType))! {
for item in items! {
if let urlString = item.string(forType: kUTTypeURL as String) {
self.webViewController.loadURL(text: urlString)
}
else
if let urlString = item.string(forType: kUTTypeFileURL as String/*"public.file-url"*/) {
let fileURL = NSURL.init(string: urlString)?.filePathURL
self.webViewController.loadURL(url: fileURL!)
}
else
{
Swift.print("items has \(item.types)")
}
}
}
else
if (pboard.types?.contains(NSPasteboardURLReadingFileURLsOnlyKey))! {
Swift.print("we have NSPasteboardURLReadingFileURLsOnlyKey")
}
return true
}
as no URL is acted upon or error thrown.
Yes, the file access is implicit. As the sandbox implementation is poorly documented and had/has many bugs, you want to work around URL and Filenames. The view should register itself for both types at initialisation. Code is in Objective-C, but API should be the same.
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
Then on performDragOperation:
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
BOOL dragPerformed = NO;
NSPasteboard *paste = [sender draggingPasteboard];
NSArray *typesWeRead = [NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil];
//a list of types that we can accept
NSString *typeInPasteboard = [paste availableTypeFromArray:typesWeRead];
if ([typeInPasteboard isEqualToString:NSFilenamesPboardType]) {
NSArray *fileArray = [paste propertyListForType:#"NSFilenamesPboardType"];
//be careful since this method returns id.
//We just happen to know that it will be an array. and it contains strings.
NSMutableArray *urlArray = [NSMutableArray arrayWithCapacity:[fileArray count]];
for (NSString *path in fileArray) {
[urlArray addObject:[NSURL fileURLWithPath:path]];
}
dragPerformed = //.... do your stuff with the files;
} else if ([typeInPasteboard isEqualToString:NSURLPboardType]) {
NSURL *droppedURL = [NSURL URLFromPasteboard:paste];
if ([droppedURL isFileURL]) {
dragPerformed = //.... do your stuff with the files;
}
}
return dragPerformed;
}

How to get an ALAsset URL from a PHAsset?

You can do it sneakily† using the undocumented PHAsset.ALAssetURL property, but I'm looking for something documented.
† In Objective-C, this will help
#interface PHAsset (Sneaky)
#property (nonatomic, readonly) NSURL *ALAssetURL;
#end
Create the assetURL by leveraging the localidentifier of the PHAsset.
Example:
PHAsset.localidentifier returns 91B1C271-C617-49CE-A074-E391BA7F843F/L0/001
Now take the 32 first characters to build the assetURL, like:
assets-library://asset/asset.JPG?id=91B1C271-C617-49CE-A074-E391BA7F843F&ext=JPG
You might change the extension JPG depending on the UTI of the asset (requestImageDataForAsset returns the UTI), but in my testing the extensions of the assetURL seems to be ignored anyhow.
I wanted to be able to get a URL for an asset too. However, I have realised that the localIdentifier can be persisted instead and used to recover the PHAsset.
PHAsset* asset = [PHAsset fetchAssetsWithLocalIdentifiers:#[localIdentifier] options:nil].firstObject;
Legacy asset URLs can be converted using:
PHAsset* legacyAsset = [PHAsset fetchAssetsWithALAssetUrls:#[assetUrl] options:nil].firstObject;
NSString* convertedIdentifier = legacyAsset.localIdentifier;
(before that method gets obsoleted...)
(Thanks holtmann - localIdentifier is hidden away in PHObject.)
Here is working code tested on iOS 11 both simulator and device
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
[asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
NSLog(#"URL:%#", contentEditingInput.fullSizeImageURL.absoluteString);
NSString* path = [contentEditingInput.fullSizeImageURL.absoluteString substringFromIndex:7];//screw all the crap of file://
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:path];
if (isExist)
NSLog(#"oh yeah");
else {
NSLog(#"damn");
}
}];
}];
Read the bottom!
The resultHandler for PHImageManager.requestImage returns 2 objects: result and info.
You can get the original filename for the PHAsset (like IMG_1043.JPG) as well as its full path on the filesystem with:
let url = info?["PHImageFileURLKey"] as! URL
This should work right, but for some reason it doesn't. So basically, you have to copy your image to a file then access that then delete it.
The PHImageFileURLKey is usable to get the original file name, but you cannot actually access that file. It probably has to do with the fact that code in the background can access the file while other apps can delete it.
Here is a PHAsset extension written in Swift that will retrieve the URL.
extension PHAsset {
func getURL(completionHandler : #escaping ((_ responseURL : URL?) -> Void)){
if self.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
})
} else if self.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl: URL = urlAsset.url as URL
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
}

Get "GeneratedUID" programmatically in COCOA

I'm trying to get system GeneratedUID programmetically, i'm able to get the UID from command line using "dscl . -search /Users GeneratedUID 00052AE8-5000-6000-9007-666F6B666A66"
Can anyone help how to get this GeneratedUID programmetically?
This worked for me. It returns the first GeneratedUID that it finds for the current user, or nil if none are found.
There are lots of errors to handle, making the logic more complicated than it really should be.
-(NSString*) getGeneratedUID {
NSString* retval = nil;
ODSession *mySession = [ODSession defaultSession];
NSError *err;
ODNode *myNode = [ODNode nodeWithSession:mySession type:kODNodeTypeAuthentication error:&err];
NSString* username = NSUserName();
ODQuery *myQuery = [ODQuery queryWithNode: myNode
forRecordTypes: kODRecordTypeUsers
attribute: kODAttributeTypeRecordName
matchType: kODMatchEqualTo
queryValues: username
returnAttributes: kODAttributeTypeStandardOnly
maximumResults: 0
error: &err];
if (err) {
NSLog(#"Error creating current user query: %#", [err localizedDescription]);
}
else {
NSArray* queryResults = [myQuery resultsAllowingPartial:NO error:&err];
if (err) {
NSLog(#"Error executing current user query: %#", [err localizedDescription]);
}
else {
if ( [queryResults count] > 0 ) {
ODRecord* firstQueryResult = [queryResults objectAtIndex:0];
NSArray* uidValues = [firstQueryResult valuesForAttribute:#kDS1AttrGeneratedUID error:&err];
if (err) {
NSLog(#"Error getting GeneratedUID attribute for current user: %#", [err localizedDescription]);
}
else {
if ([uidValues count] > 0) {
retval = (NSString*) [uidValues objectAtIndex:0];
}
else {
NSLog(#"No GeneratedUID values for current username: %#", username);
}
}
}
else {
NSLog(#"No results from current user query for username: %#", username);
}
}
}
return retval;
}
just use the following code to retrive UID.... import the SecurityConfiguration.framework before you add the code..
NSString* GetConsoleUser(uid_t* p_uid, gid_t* p_gid) {
NSString* name = (NSString*)CFBridgingRelease(SCDynamicStoreCopyConsoleUser(
NULL, p_uid, p_gid));
return name ;
}
you have to play around the string to get the "UID".

NSSharingService - Remove facebook and twitter and add Print

I've just implemented a share-button, that has a share menu:
[_shareButton sendActionOn:NSLeftMouseDownMask];
And has this action connected:
-(IBAction)share:(id)sender {
NSArray *shareArray = #[#"testShare"];
NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:shareArray];
sharingServicePicker.delegate = self;
[sharingServicePicker showRelativeToRect:[sender bounds]
ofView:sender
preferredEdge:NSMinYEdge];
}
Now to my question, I don't want Facebook and Twitter to be an option in the menu. I only want E-Mail and Messages to be available. Also I would like to add "Print", but don't know if I can do that.
Is that possible?
Thanks
(Don't have enough rep points to add 'NSSharingService' as a tag)
Solved it by using proposedSharingServices.
- (NSArray *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices{
// Find and the services you want
NSMutableArray *newProposedServices = [[NSMutableArray alloc] initWithCapacity:5];
for (NSSharingService *sharingService in proposedServices) {
if ([[sharingService title] isEqualToString:#"Email"] || [[sharingService title] isEqualToString:#"Message"]) {
[newProposedServices addObject:sharingService];
}
}
NSArray *services = newProposedServices;
NSSharingService *customService = [[NSSharingService alloc] initWithTitle:#"Print" image:[NSImage imageNamed:#"PrintImage"] alternateImage:nil handler:^{
// Do whatever
}];
services = [services arrayByAddingObject:customService];
return services;
}
Comparing a proposed service to a new named instance works. Here's a trivial Swift code from my project:
let excludedNames = [
NSSharingServiceNamePostOnFacebook,
NSSharingServiceNamePostOnTwitter,
]
var excludedServices = [NSSharingService]()
for name in excludedNames {
if let service = NSSharingService(named: name) {
excludedServices += [service]
}
}
return proposedServices.filter {
!excludedServices.contains($0)
}
No need to use a private name property.
Rather then trying to say what you don't want simply return a list of what you do want.
- (NSArray<NSSharingService *> *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray<NSSharingService *> *)proposedServices
{
NSArray *result = #[[NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail], [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage]];
return result;
}
A slightly different approach via proposedSharingServices:
  
- (NSArray*)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices {
NSArray *excludedServices = #[NSSharingServiceNamePostOnFacebook,
NSSharingServiceNamePostOnTwitter];
NSArray *sharingServices = [proposedServices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"NOT (name IN %#)", excludedServices]];
return sharingServices;
}
Here's a better way - no private API access required.
NSArray *excludedServices = #[NSSharingServiceNamePostOnFacebook,
NSSharingServiceNamePostOnTwitter];
NSMutableArray *includedServices = [[NSMutableArray alloc] init];
for (NSSharingService *service in proposedServices) {
if ([excludedServices indexOfObject:service] == NSNotFound) {
[includedServices addObject:service];
}
}
return includedServices;

NSFileManager delete contents of directory

How do you delete all the contents of a directory without deleting the directory itself? I want to basically empty a folder yet leave it (and the permissions) intact.
E.g. by using a directory enumerator:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:path];
NSString *file;
while (file = [enumerator nextObject]) {
NSError *error = nil;
BOOL result = [fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error];
if (!result && error) {
NSLog(#"Error: %#", error);
}
}
Swift
let fileManager = NSFileManager.defaultManager()
let enumerator = fileManager.enumeratorAtURL(cacheURL, includingPropertiesForKeys: nil, options: nil, errorHandler: nil)
while let file = enumerator?.nextObject() as? String {
fileManager.removeItemAtURL(cacheURL.URLByAppendingPathComponent(file), error: nil)
}
Try this:
NSFileManager *manager = [NSFileManager defaultManager];
NSString *dirToEmpty = ... //directory to empty
NSError *error = nil;
NSArray *files = [manager contentsOfDirectoryAtPath:dirToEmpty
error:&error];
if(error) {
//deal with error and bail.
}
for(NSString *file in files) {
[manager removeItemAtPath:[dirToEmpty stringByAppendingPathComponent:file]
error:&error];
if(error) {
//an error occurred...
}
}
in swift 2.0:
if let enumerator = NSFileManager.defaultManager().enumeratorAtPath(dataPath) {
while let fileName = enumerator.nextObject() as? String {
do {
try NSFileManager.defaultManager().removeItemAtPath("\(dataPath)\(fileName)")
}
catch let e as NSError {
print(e)
}
catch {
print("error")
}
}
}
Swift 2.1.1:
public func deleteContentsOfFolder()
{
// folderURL
if let folderURL = self.URL()
{
// enumerator
if let enumerator = NSFileManager.defaultManager().enumeratorAtURL(folderURL, includingPropertiesForKeys: nil, options: [], errorHandler: nil)
{
// item
while let item = enumerator.nextObject()
{
// itemURL
if let itemURL = item as? NSURL
{
do
{
try NSFileManager.defaultManager().removeItemAtURL(itemURL)
}
catch let error as NSError
{
print("JBSFile Exception: Could not delete item within folder. \(error)")
}
catch
{
print("JBSFile Exception: Could not delete item within folder.")
}
}
}
}
}
}
Swift 3 if anyone needs it for a quick cut/paste
let fileManager = FileManager.default
let fileUrls = fileManager.enumerator(at: folderUrl, includingPropertiesForKeys: nil)
while let fileUrl = fileUrls?.nextObject() {
do {
try fileManager.removeItem(at: fileUrl as! URL)
} catch {
print(error)
}
}
The documentation for contentsOfDirectoryAtPath:error: says:
The search is shallow and therefore does not return the contents of any subdirectories. This returned array does not contain strings for the current directory (“.”), parent directory (“..”), or resource forks (begin with “._”) and does not traverse symbolic links.
Thus:
---( file != #"." && file != #".." )---
is irrelevant.
You can extend the NSFileManager like this:
extension NSFileManager {
func clearFolderAtPath(path: String) -> Void {
for file in subpathsOfDirectoryAtPath(path, error: nil) as? [String] ?? [] {
self.removeItemAtPath(path.stringByAppendingPathComponent(file), error: nil)
}
}
}
Then, you can clear the folder like this: NSFileManager.defaultManager().clearFolderAtPath("the folder's path")
Georg Fritzsche answer for Swift did not work for me. Instead of reading the enumerated object as a String, read it as NSURL.
let fileManager = NSFileManager.defaultManager()
let url = NSURL(string: "foo/bar")
let enumerator = fileManager.enumeratorAtURL(url, includingPropertiesForKeys: nil, options: nil, errorHandler: nil)
while let file = enumerator?.nextObject() as? NSURL {
fileManager.removeItemAtURL(file, error: nil)
}
Why not deleting the whole directory and recreate afterwards? Just get the file attributes and permissions before deleting it, and then recreate it with the same attributes.

Resources