React-native-fs: is there anyway to read directory recursively to get sub folder and items in it

Created on 3 Dec 2018  路  2Comments  路  Source: itinance/react-native-fs

I can able to read the files in a directory using following function

        const path = RNFS.DocumentDirectoryPath;

        RNFS.readDir(path)
        .then((result) => {
            console.warn(result);
            const Files = [];

            if(result != undefined && result.length > 0){

                for(index in result) {
                    const item = result[index];
                    console.log(item);

                    if(item.isFile()){

                        Files.push(item);
                    }
                }

                if(Files.length > 0) {

                    callback(Files);
                }
                else{

                    callback(null);
                }
            }
            else{
                callback(null);
            }
        })
        .catch(err => {

            console.log('err in reading files');
            console.log(err);
            callback(null);
        })

But i want to read the directory and it's subdirectory and the files belongs to them, is there any way to achieve it?

Most helpful comment

I've achieved copying recursively from something in assets, to something in the document directory, like this:

import FileSystem from "react-native-fs";

...

async copyRecursive(source, destination) {
    console.log(`${source} => ${destination}`);
    const items = await FileSystem.readDirAssets(source);

    console.log(`mkdir ${destination}/`);
    await FileSystem.mkdir(destination);

    await items.forEach(async item => {
      if (item.isFile()) {
        console.log(`f ${item.path}`);

        const destPath =
          FileSystem.DocumentDirectoryPath + "/" + source + "/" + item.name;

        console.log(`cp ${item.path} ${destPath}`);
        await FileSystem.copyFileAssets(item.path, destPath);
      } else {
        console.log(`d ${item.path}/`);

        // Restart with expanded path
        const subDirectory = source + "/" + item.name;
        const subDestination = destination + "/" + item.name;
        await this.copyRecursive(subDirectory, subDestination);
      }
    });

    this.setState({
      androidReady: true
    });
}

Example result output:

www => /data/user/0/com.myapp/files/www
mkdir /data/user/0/com.myapp/files/www/
f www/index.html
cp www/index.html /data/user/0/com.myapp/files/www/index.html
d www/subfolder/
www/subfolder => /data/user/0/com.myapp/files/www/subfolder
mkdir /data/user/0/com.myapp/files/www/subfolder/
f www/subfolder/subfile.html
cp www/subfolder/subfile.html /data/user/0/com.myapp/files/www/subfolder/subfile.html
d www/subfolder/subsubfolder/
www/subfolder/subsubfolder => /data/user/0/com.myapp/files/www/subfolder/subsubfolder
mkdir /data/user/0/com.myapp/files/www/subfolder/subsubfolder/
f www/subfolder/subsubfolder/subsubfile.ts
cp www/subfolder/subsubfolder/subsubfile.ts /data/user/0/com.myapp/files/www/subfolder/subsubfolder/subsubfile.ts

All 2 comments

I've achieved copying recursively from something in assets, to something in the document directory, like this:

import FileSystem from "react-native-fs";

...

async copyRecursive(source, destination) {
    console.log(`${source} => ${destination}`);
    const items = await FileSystem.readDirAssets(source);

    console.log(`mkdir ${destination}/`);
    await FileSystem.mkdir(destination);

    await items.forEach(async item => {
      if (item.isFile()) {
        console.log(`f ${item.path}`);

        const destPath =
          FileSystem.DocumentDirectoryPath + "/" + source + "/" + item.name;

        console.log(`cp ${item.path} ${destPath}`);
        await FileSystem.copyFileAssets(item.path, destPath);
      } else {
        console.log(`d ${item.path}/`);

        // Restart with expanded path
        const subDirectory = source + "/" + item.name;
        const subDestination = destination + "/" + item.name;
        await this.copyRecursive(subDirectory, subDestination);
      }
    });

    this.setState({
      androidReady: true
    });
}

Example result output:

www => /data/user/0/com.myapp/files/www
mkdir /data/user/0/com.myapp/files/www/
f www/index.html
cp www/index.html /data/user/0/com.myapp/files/www/index.html
d www/subfolder/
www/subfolder => /data/user/0/com.myapp/files/www/subfolder
mkdir /data/user/0/com.myapp/files/www/subfolder/
f www/subfolder/subfile.html
cp www/subfolder/subfile.html /data/user/0/com.myapp/files/www/subfolder/subfile.html
d www/subfolder/subsubfolder/
www/subfolder/subsubfolder => /data/user/0/com.myapp/files/www/subfolder/subsubfolder
mkdir /data/user/0/com.myapp/files/www/subfolder/subsubfolder/
f www/subfolder/subsubfolder/subsubfile.ts
cp www/subfolder/subsubfolder/subsubfile.ts /data/user/0/com.myapp/files/www/subfolder/subsubfolder/subsubfile.ts

I think the way you've done it is the way to go, looping through each one.

For my needs, we needed to find the size of a directory, recursively. We tried looping through and that was taking way too long on larger directories (>1Gb).

We switched to using a custom Native Module and calling du -hsc like this:

String command = "ls " + path; // Where path is your desired path.

Runtime runtime = Runtime.getRuntime();

Process result = runtime.exec( command );

BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( result.getInputStream() ) );

String[] parts = bufferedReader.readLine().split( "\\s+" );

That was fast but still taking too long.

So we ended up using StatFs on the directory, like this:

StatFs statFs = new StatFs(directory.getAbsolutePath());

And that gets you a quick and dirty estimate of the directory size without looping through each directory. Much faster.

Hopefully that helps somebody else.

In your case, I'd say this Issue can be closed since it's not an _issue_ with react-native-fs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

crobinson42 picture crobinson42  路  4Comments

let-aurn picture let-aurn  路  3Comments

ToJen picture ToJen  路  4Comments

AbdelkaderOumrani picture AbdelkaderOumrani  路  3Comments

wangfpp picture wangfpp  路  3Comments