NSFileManager 无法正常工作
NSFileManager does not properly work
我想用 NSFileManager
移动 2 个文件,但它没有移动我的文件:
在 myfolder
有 5 个文本文件,我将删除所有这些文件
我的代码:
BOOL isDir;
if([[NSFileManager defaultManager] fileExistsAtPath:@"/volume/netrt/myfolder/" isDirectory:&isDir]) {
[[NSFileManager defaultManager] removeItemAtPath:@"/volume/netrt/myfolder/*" error:nil]; // for delete all files
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text1.txt" toPath:@"/volume/netrt/myfolder/" error:nil];
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text2.txt" toPath:@"/volume/netrt/myfolder/" error:nil];
}
文件夹 myfolder
已经存在。我的错误是什么?
非常感谢
很可能还有其他问题,但对于初学者来说,toPath:
需要一个实际的文件名。
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text1.txt" toPath:@"/volume/netrt/myfolder/text1.txt" error:nil];
将 "text1.txt" 添加到 toPath:
的末尾。
您知道,error
参数不是您可以忽略的。您遇到问题但仍然通过 nil
?!认真的吗?
如果你已经看过 NSFileManager
告诉你的错误是你就不需要问这个问题了。
下面是演示游乐场的代码,您应该可以使用它来解决您的问题。
// Demo Playground for Whosebug Question
//
// Shows how to recursively remove all file in a directory by removing the directory and then recreating it.
// Please see the main function first for more explanation
import Cocoa
//Creates Fake Directory with Fake Files
func createFakeDirectoryAtPath(path: String) -> NSError? {
var isDir = ObjCBool(false)
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) {
println("INFO> Will not create fake directory, file exists at: \(path).")
return nil
}
var createDirError: NSError?
NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: false, attributes: nil, error: &createDirError)
if let error = createDirError {
return error
}
println("\tINFO> Created Fake Directory Named: \(path.lastPathComponent).")
for filename in ["one.txt", "two.jpg", "three.png", "four.doc", "five.txt", "six.mp3"] {
let filepath = path.stringByAppendingPathComponent(filename)
if NSFileManager.defaultManager().createFileAtPath(filepath, contents: nil, attributes: nil) {
println("\t\tINFO> Created Fake File Named: \(filename).")
}
}
return nil
}
func createFakeDirectoryStructureAtPath(path: String) -> Bool {
//Create base directory
let createDirError = createFakeDirectoryAtPath(path)
if let error = createDirError {
println("!ERROR> " + error.localizedDescription)
return false
}
//Creates fake directories
for dirname in ["one", "two", "three", "four", "five", "six"] {
let subDirPath = path.stringByAppendingPathComponent(dirname)
let createSubDirError = createFakeDirectoryAtPath(subDirPath)
if let error = createSubDirError {
println("!ERROR> " + error.localizedDescription)
return false
}
}
return true
}
//Removes a directory at all it's contents
func removeDirectoryAtPath(path: String) {
var isDir = ObjCBool(false)
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) && isDir {
var removeDirError: NSError?
NSFileManager.defaultManager().removeItemAtPath(path, error: &removeDirError)
if let error = removeDirError {
println("!ERROR> " + error.localizedDescription)
}
}
}
//This is where the demo execution will begin.
// Start by opening the path that will contain the fake directory.
// Then one by one turn use the boolean controls to turn on each step and explore the results of each step.
// Then look at the code to figure out how it's doing what it's doing.
func main() {
//Controls Location of Fake Directory
let fakeDirectoryPath = "~/fake-directory".stringByExpandingTildeInPath
assert(fakeDirectoryPath.lastPathComponent == "fake-directory", "This code will remove the directory and all contents in the directory at path: \(fakeDirectoryPath). If you are ABSOLUTELY sure you want to do this, please update this assertion. Note playground code will automatically execute so you could go up a creek without a paddle if you remove this assertion.")
println("Open this path: \( fakeDirectoryPath.stringByDeletingLastPathComponent)\n\n")
//These Booleans control whether or not execute a step of the demo.
let setupFakeDirectory = false
let removeFakeDirectory = false
let createEmptyDirectory = false
let removeEmptyDirectory = false
//Create Fake Directory and Subdirectories with fake files in them.
if setupFakeDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
let success = createFakeDirectoryStructureAtPath(fakeDirectoryPath)
if success {
println("Created Fake Directory Structure at Path: \(fakeDirectoryPath)")
} else {
println("Didn't Create Fake Directory Structure At Path: \(fakeDirectoryPath)")
}
}
//Removes the fake directory structure
if removeFakeDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
}
if createEmptyDirectory {
var createDirError: NSError?
NSFileManager.defaultManager().createDirectoryAtPath(fakeDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirError)
if let error = createDirError {
println("!ERROR> " + error.localizedDescription)
}
}
if removeEmptyDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
}
}
main()
我想用 NSFileManager
移动 2 个文件,但它没有移动我的文件:
在 myfolder
有 5 个文本文件,我将删除所有这些文件
我的代码:
BOOL isDir;
if([[NSFileManager defaultManager] fileExistsAtPath:@"/volume/netrt/myfolder/" isDirectory:&isDir]) {
[[NSFileManager defaultManager] removeItemAtPath:@"/volume/netrt/myfolder/*" error:nil]; // for delete all files
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text1.txt" toPath:@"/volume/netrt/myfolder/" error:nil];
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text2.txt" toPath:@"/volume/netrt/myfolder/" error:nil];
}
文件夹 myfolder
已经存在。我的错误是什么?
非常感谢
很可能还有其他问题,但对于初学者来说,toPath:
需要一个实际的文件名。
[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text1.txt" toPath:@"/volume/netrt/myfolder/text1.txt" error:nil];
将 "text1.txt" 添加到 toPath:
的末尾。
您知道,error
参数不是您可以忽略的。您遇到问题但仍然通过 nil
?!认真的吗?
如果你已经看过 NSFileManager
告诉你的错误是你就不需要问这个问题了。
下面是演示游乐场的代码,您应该可以使用它来解决您的问题。
// Demo Playground for Whosebug Question
//
// Shows how to recursively remove all file in a directory by removing the directory and then recreating it.
// Please see the main function first for more explanation
import Cocoa
//Creates Fake Directory with Fake Files
func createFakeDirectoryAtPath(path: String) -> NSError? {
var isDir = ObjCBool(false)
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) {
println("INFO> Will not create fake directory, file exists at: \(path).")
return nil
}
var createDirError: NSError?
NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: false, attributes: nil, error: &createDirError)
if let error = createDirError {
return error
}
println("\tINFO> Created Fake Directory Named: \(path.lastPathComponent).")
for filename in ["one.txt", "two.jpg", "three.png", "four.doc", "five.txt", "six.mp3"] {
let filepath = path.stringByAppendingPathComponent(filename)
if NSFileManager.defaultManager().createFileAtPath(filepath, contents: nil, attributes: nil) {
println("\t\tINFO> Created Fake File Named: \(filename).")
}
}
return nil
}
func createFakeDirectoryStructureAtPath(path: String) -> Bool {
//Create base directory
let createDirError = createFakeDirectoryAtPath(path)
if let error = createDirError {
println("!ERROR> " + error.localizedDescription)
return false
}
//Creates fake directories
for dirname in ["one", "two", "three", "four", "five", "six"] {
let subDirPath = path.stringByAppendingPathComponent(dirname)
let createSubDirError = createFakeDirectoryAtPath(subDirPath)
if let error = createSubDirError {
println("!ERROR> " + error.localizedDescription)
return false
}
}
return true
}
//Removes a directory at all it's contents
func removeDirectoryAtPath(path: String) {
var isDir = ObjCBool(false)
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) && isDir {
var removeDirError: NSError?
NSFileManager.defaultManager().removeItemAtPath(path, error: &removeDirError)
if let error = removeDirError {
println("!ERROR> " + error.localizedDescription)
}
}
}
//This is where the demo execution will begin.
// Start by opening the path that will contain the fake directory.
// Then one by one turn use the boolean controls to turn on each step and explore the results of each step.
// Then look at the code to figure out how it's doing what it's doing.
func main() {
//Controls Location of Fake Directory
let fakeDirectoryPath = "~/fake-directory".stringByExpandingTildeInPath
assert(fakeDirectoryPath.lastPathComponent == "fake-directory", "This code will remove the directory and all contents in the directory at path: \(fakeDirectoryPath). If you are ABSOLUTELY sure you want to do this, please update this assertion. Note playground code will automatically execute so you could go up a creek without a paddle if you remove this assertion.")
println("Open this path: \( fakeDirectoryPath.stringByDeletingLastPathComponent)\n\n")
//These Booleans control whether or not execute a step of the demo.
let setupFakeDirectory = false
let removeFakeDirectory = false
let createEmptyDirectory = false
let removeEmptyDirectory = false
//Create Fake Directory and Subdirectories with fake files in them.
if setupFakeDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
let success = createFakeDirectoryStructureAtPath(fakeDirectoryPath)
if success {
println("Created Fake Directory Structure at Path: \(fakeDirectoryPath)")
} else {
println("Didn't Create Fake Directory Structure At Path: \(fakeDirectoryPath)")
}
}
//Removes the fake directory structure
if removeFakeDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
}
if createEmptyDirectory {
var createDirError: NSError?
NSFileManager.defaultManager().createDirectoryAtPath(fakeDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirError)
if let error = createDirError {
println("!ERROR> " + error.localizedDescription)
}
}
if removeEmptyDirectory {
removeDirectoryAtPath(fakeDirectoryPath)
}
}
main()