NSFileManager does not properly work

169 views Asked by At

I want to move 2 files with NSFileManager, but it isn't moving my files:

at myfolder There are 5 text file, that i will remove all this files

My code:

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];
}

The folder myfolder already exists. What is my mistake?

thank you so much

3

There are 3 answers

14
Bek On BEST ANSWER

There's very likely other issues going on, but for starters, the toPath: needs an actual file name.

[[NSFileManager defaultManager] movItemAtPath:@"/rdns/macross/text1.txt" toPath:@"/volume/netrt/myfolder/text1.txt" error:nil];

Add "text1.txt" to the end of your toPath:.

NSFileManager documentation

0
Christian Schnorr On

You know, the error parameter isn't there for you to ignore. You are encountering an issue and still passing nil?! Seriously?

If you had looked at what the NSFileManager told you the error was you wouldn't need to ask this question.

7
Tobias On

Below is code for a demo playground that you should be able to use to figure out your question.

// Demo Playground for StackOverflow Question
// http://stackoverflow.com/questions/30807497/nsfilemanager-does-not-properly-work/30807624?noredirect=1#comment50017381_30807624

// 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()