所以,我需要在目录中搜索Unix Executable files
。我遍历目录和我正在搜索的文件的路径。我试过的一些方法。如何以编程方式在OS X中搜索unix可执行文件?
1.当文件扩展名
Unix Executable file
没有文件扩展名的帮助,但有些文件的文件也没有扩展名。所以,它失败了。
2.用的NSFileManager
NSDicitionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
的帮助,它不具有任何独特属性找到Unix的可执行文件。
3. MDItemRef的帮助
它具有属性称为kMDItemContentType
但它给一些只unix executable files
的正确结果。
MDItemRef inspectedRef;
CFArrayRef inspectedRefAttributeNames;
CFDictionaryRef inspectedRefAttributeValues;
inspectedRef = MDItemCreate(kCFAllocatorDefault,(CFStringRef)filePath);
if(inspectedRef) {
inspectedRefAttributeNames = MDItemCopyAttributeNames(inspectedRef);
inspectedRefAttributeValues = MDItemCopyAttributes(inspectedRef,inspectedRefAttributeNames);
NSDictionary *attribDict = (__bridge NSDictionary*)inspectedRefAttributeValues;
if([[attribDict objectForKey:@"kMDItemContentType"] isEqualToString:@"public.unix-executable"])
NSLog(@"Unix Executable file");
}
4.用unix命令的帮助 “文件”
NSTask *unixTask = [[NSTask alloc] init];
[unixTask setStandardOutput:newPipe];
[unixTask setLaunchPath:@"/usr/bin/file"];
[unixTask setArguments:[NSArray arrayWithObject:filePath]];
[unixTask launch];
[unixTask waitUntilExit];
[unixTask terminationStatus];
while ((inData = [readHandle availableData]) && [inData length]) {
returnValue= [[NSString alloc] initWithData:inData encoding:[NSString defaultCStringEncoding]];
returnValue = [returnValue substringToIndex:[returnValue length]-1];
NSLog(@"%@",returnValue);
}
这里,从returnValue
我能找到它是否是UNIX可执行与否。但过程非常缓慢。所以,我的问题是如何以有效的方式搜索unix可执行文件?
感谢您的回答。对于文档类型文件也显示为Unix可执行文件:( – Aravindhan 2012-08-10 08:32:59
我不知道你以前的评论意味着什么。你的意思是NSURLTypeIdentifierKey属性是“public.unix-executable”,但查找器列出文件为“文件“?这听起来很奇怪。 – 2012-08-10 09:32:14
雅..我不是为什么它显示文件文件为unix可执行文件。 – Aravindhan 2012-08-10 09:40:20