React-native-fs: Android move and copy always says ENOENT: no such file or directory

Created on 10 Sep 2016  路  2Comments  路  Source: itinance/react-native-fs

Hello,

With Android and RN 0.30 I've an issue with this module (2.0.1-rc2). Here is an example briefly : (I'll not write code, just illustrate the example).

mkdir('/tmp/a/10');
mkdir('/tmp/b/20');

So basically 2 folders with 2 folders within.

Now If I try to copyFile or moveFile like that :

moveFile('/tmp/b','/tmp/a')

I got an ENOENT: no such file or directory error

At start I just wanted to rename folder b to a but with this module I can't. Do you have any idea ?

Thanks !

Most helpful comment

Here it is ! Recursive move/rename functions. One drawback, you don't know when it's done.

module.exports = {

    moveDirectorySafely: function(src, dst) {
        RNFS.exists(dst).then((dirExists) => {
            if (dirExists) {
                RNFS.unlink(dst).then(() => {
                    this.moveDirectory(src, dst);
                });
            } else {
                this.moveDirectory(src, dst);
            }
        });
    },

    moveDirectory: function(src, dst, root = true) {
        if (root) {
            src = RNFS.DocumentDirectoryPath + src;
            dst = RNFS.DocumentDirectoryPath + dst;
        }

        RNFS.mkdir(dst).then(() => {
            RNFS.readDir(src)
                .then((files) => {
                    for (let i = 0; i < files.length; i++) {
                        const name = files[i].name;
                        const filepath = src + '/' + name;
                        const destpath = src.replace(src, dst);
                        const destfile = destpath + '/' + name;

                        if (files[i].isDirectory()) {
                            RNFS.mkdir(destfile).then(() => {
                                this.moveDirectory(filepath, destfile, false);
                            });
                        } else {
                            RNFS.copyFile(filepath, destfile);
                        }
                    }
                }).catch((err) => {
                    console.log(err);
                });
        });
    }

};


All 2 comments

I found.. (rtfm) This is just a copyFile or moveFile.

So if you want to move an entire folder you can't, you'll have to code a recursive function with readFile. I'll edit my answer soon with such a function.

Still waiting for a rename folder method by the way :)

Here it is ! Recursive move/rename functions. One drawback, you don't know when it's done.

module.exports = {

    moveDirectorySafely: function(src, dst) {
        RNFS.exists(dst).then((dirExists) => {
            if (dirExists) {
                RNFS.unlink(dst).then(() => {
                    this.moveDirectory(src, dst);
                });
            } else {
                this.moveDirectory(src, dst);
            }
        });
    },

    moveDirectory: function(src, dst, root = true) {
        if (root) {
            src = RNFS.DocumentDirectoryPath + src;
            dst = RNFS.DocumentDirectoryPath + dst;
        }

        RNFS.mkdir(dst).then(() => {
            RNFS.readDir(src)
                .then((files) => {
                    for (let i = 0; i < files.length; i++) {
                        const name = files[i].name;
                        const filepath = src + '/' + name;
                        const destpath = src.replace(src, dst);
                        const destfile = destpath + '/' + name;

                        if (files[i].isDirectory()) {
                            RNFS.mkdir(destfile).then(() => {
                                this.moveDirectory(filepath, destfile, false);
                            });
                        } else {
                            RNFS.copyFile(filepath, destfile);
                        }
                    }
                }).catch((err) => {
                    console.log(err);
                });
        });
    }

};


Was this page helpful?
0 / 5 - 0 ratings