Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Created November 17, 2014 10:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/4fcafe793b4f120fbf48 to your computer and use it in GitHub Desktop.
Save norio-nomura/4fcafe793b4f120fbf48 to your computer and use it in GitHub Desktop.
NSURLIsExcludedFromBackupKeyがtrueなものをリストアップする
#!/usr/bin/env xcrun swift
import Cocoa
extension NSURL {
var isDirectory: Bool {
return resourceValue(NSURLIsDirectoryKey)
}
var isExcludedFromBackup: Bool {
return resourceValue(NSURLIsExcludedFromBackupKey)
}
func resourceValue(resourceKey: NSString) -> Bool {
var resourceValue: AnyObject?
var error: NSError?
var result = false
if getResourceValue(&resourceValue, forKey: resourceKey, error: &error) {
if let number = resourceValue as? NSNumber {
result = number.boolValue
} else {
println("error: \(self) for key: \(resourceKey)")
}
} else {
println("error: \(error?.localizedDescription) on \(self) for key: \(resourceKey)")
}
return result
}
}
func printExcludedFromBackupContentsAtURL(targetURL: NSURL) {
let keys = [NSURLIsDirectoryKey, NSURLIsExcludedFromBackupKey] as [AnyObject]
var error: NSError?
if let contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(targetURL, includingPropertiesForKeys: keys, options: nil, error: &error) as? [NSURL] {
for url in contents {
if url.isExcludedFromBackup {
println(url.path!)
} else if url.isDirectory {
printExcludedFromBackupContentsAtURL(url)
}
}
} else {
println("\(targetURL.path!): \(error!.localizedDescription)")
}
}
var path = Process.arguments.count > 1 ? Process.arguments[1] : NSFileManager.defaultManager().currentDirectoryPath
if let url = NSURL(fileURLWithPath: path) {
printExcludedFromBackupContentsAtURL(url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment