我在重命名和移动文件夹时遇到了麻烦,因为React Native fs仅具有仅对文件而不对文件夹执行的moveFile选项。递归也很麻烦,由于同步执行,在执行重命名或移动选项后很难执行行。已将错误的附加代码附在下面。请帮我解决这个问题。
moveAll = (path, outputPath) => new Promise((resolve, reject) => {
// is a folder
if (path.split(".").length == 1) {
// CHeck if folder already exists
RNFS.exists(outputPath)
.then((exists) => {
if (exists) {
// Delete the folder if exists
RNFS.unlink(outputPath)
.then(() => {
})
// `unlink` will throw an error, if the item to unlink does not exist
.catch((err) => {
console.log(err.message);
});
}
// MAKE FRESH FOLDER
RNFS.mkdir(outputPath);
resolve(RNFS.readDir(path)
.then((result) => {
result.map(
(item) =>
new Promise((resolve, reject) => {
resolve(this.moveAll(item.path, outputPath + "/" + item.name));
})
)
})
.catch((e) => {
console.log("ERROR", e)
})
)
})
.catch((e) => {
console.log(e)
})
} else {
RNFS.moveFile(path, outputPath)
.then(() => {
})
.catch((e) => {
console.log(e)
})
}
})
提前致谢 :)
感谢您的帮助,我为您的解决方案找到了答案
moveAll = async (path, outputPath) => {
// is a folder
if (path.split(".").length == 1) {
// CHeck if folder already exists
var exists = await RNFS.exists(outputPath);
if (exists) {
await RNFS.unlink(outputPath);
await RNFS.mkdir(outputPath);
}
// MAKE FRESH FOLDER
var result = await RNFS.readDir(path);
for (var i = 0; i < result.length; i++) {
if (result[i].isDirectory()) {
await RNFS.mkdir(outputPath + "/" + result[i].name);
}
var val = await this.moveAll(result[i].path, outputPath + "/" + result[i].name);
}
await RNFS.unlink(path);
return 1;
} else {
await RNFS.moveFile(path, outputPath);
return 1;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句