0

I am trying to use react-native-fs for some basic file storage. Here is some code.

var RNFS = require('react-native-fs');

static testSave() {
    var count = this.countFiles(".txt");
    var path = RNFS.DocumentDirectoryPath + '/test' + count + '.txt';
    console.log("PATH: " + path);

    // write the file
    RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
    .then((success) => {
        console.log('FILE WRITTEN!');
    })
    .catch((err) => {
        console.log(err.message);
    });
}

static countFiles(extension) {
    var allNames = RNFS.readdir(RNFS.DocumentDirectoryPath);
    var count = 0;
    for(var name in allNames) {
        if (name.includes(extension)) {
            ++count;
        }
    }
    console.log("DIR COUNT: " + count);
    return count;
}

So, I have attempted using both DocumentDirectoryPath and ExternalStorageDirectoryPath, as suggested by an answer here: react native fs library not writing files

I cannot find these files on my phone despite it saying they are being saved. Also here are some runtime logs of me executing testSave() twice (in two different react-native program executions).

12-19 21:47:46.920 30468  4162 I ReactNativeJS: Running application "AwesomeProject"          
12-19 21:47:46.961 30468  4162 I ReactNativeJS: DIR COUNT: 0
12-19 21:47:46.961 30468  4162 I ReactNativeJS: PATH: /data/user/0/com.awesomeproject/files/test0.txt
12-19 21:47:47.165 30468  4162 I ReactNativeJS: FILE WRITTEN!

12-19 21:47:51.345 30468  4174 I ReactNativeJS: Running application "AwesomeProject" 
12-19 21:47:51.389 30468  4174 I ReactNativeJS: DIR COUNT: 0
12-19 21:47:51.389 30468  4174 I ReactNativeJS: PATH: /data/user/0/com.awesomeproject/files/test0.txt
12-19 21:47:51.596 30468  4174 I ReactNativeJS: FILE WRITTEN!

1 Answers1

0

I figured out at least part of my issue. I'm not sure why directory count was coming up as 0. However, I have found the files. I'm not sure what the absolute path is, but when I open my Samsung Galaxy 8 file explorer the files are located at My Files > Internal storage/ or at My Files > Documents > Internal storage/.

I thought that I had done some searching through some of these directories, but maybe I had not searched the correct places. Sorry for the junk question!