nukl
nukl

Reputation: 10521

Move directory to trash

I need to move a directory, including its content, to the trash. I found NSWorkspaceRecycleOperation in the documentation, and wrote this code:

NSString *path = [NSString stringWithString:@"/Users/test/Desktop/test"];

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                                             source:path 
                                        destination:@"" 
                                              files:dirContents 
                                                tag:nil];

It moves all the content to the trash, but not the directory itself. So, how can I do this?

Upvotes: 1

Views: 2377

Answers (1)

Georg Fritzsche
Georg Fritzsche

Reputation: 98984

You are currently only performing the recycle operation on the directories contents. Given a directory dir to trash, use something like the following instead:

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                               source:[dir stringByDeletingLastPathComponent]
                               destination:@"" 
                               files:[NSArray arrayWithObject:[dir lastPathComponent]]
                               tag:nil];

Upvotes: 9

Related Questions