0

I want to run this command via NSTask: mv /User/xxx.deb /User/Docs To move the deb file to /User/Docs. However, the deb file can have many names: blah.deb, blah3.deb, hgfdo.deb, 6745sdjiz.deb,...

Here is my current code:

#import <Foundation/NSTask.h>

NSString *myString = @"deb";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString];
NSTask *task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/bin/mv"];
[task setArguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"/User/%@", predicate], @"/User/Tweak/", nil]];
[task launch];

And that doesn't work. Without the NSPredicate, it works:

[task setArguments:[NSArray arrayWithObjects:@"/User/com.deb", @"/User/Tweak/", nil]];

But I need to use the NSPredicate (or any other method)

Do you have an idea?

Ziph0n
  • 197
  • 1
  • 1
  • 7

1 Answers1

1

NSPredicate should be used to evaluate Objective-C objects (NSArrays, NSStrings, etc). I'd use something like:

[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL URLWithString:@"/User/"] includingPropertiesForKeys:@[] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]

to get an array all the files in /User/.

Then use NSArray's filteredArrayUsingPredicate: with an NSPredicate (a bit different than yours though something like SELF.pathExtension == %@).

You'll end up with an array of NSURLs that match your predicate. You just need to convert them to paths and add your /Tweak/ folder to the array. Which you use as the task's arguments.

Of course, if you're just trying to move some files, you don't need to use NSTask. NSFileManager has methods for moving an array of NSURLs from one directory to another.

cramopy
  • 3,459
  • 6
  • 28
  • 42