Reputation: 3358
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?
Upvotes: 3
Views: 1664
Reputation: 913
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
});
}
Upvotes: 2