如何在 React Native fs 中重命名/移动文件夹?
How to rename / move a folder in react native fs?
我在重命名和移动文件夹时遇到问题,因为 react native fs 只有 moveFile 选项只对文件执行而不是 folders.Doing 它递归也很混乱,执行重命名或移动后很难执行行由于同步 execution.I 的选项已附上下面的移动代码 bug.Kindly 帮助我解决这个问题。
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)
})
}
})
提前致谢:)
你可以调用this.moveDirectorySafely("source","destination");
递归move/rename
确保您拥有允许的存储权限
如果你有任何错误,那么你可以调用RNFS.readDir(RNFS.DownloadDirectoryPath).then
来检查这个文件夹是否存在
import React, {Component} from 'react';
import {View} from 'react-native';
var RNFS = require('react-native-fs');
export default class index extends Component {
firstSource = '';
componentDidMount() {
this.moveDirectorySafely('source', 'destination'); //you can call this function
}
moveDirectorySafely = (src, dst) => {
RNFS.exists(dst).then((dirExists) => {
if (dirExists) {
RNFS.unlink(dst).then(() => {
this.moveDirectory(src, dst);
});
} else {
this.moveDirectory(src, dst);
}
});
};
moveDirectory = (src, dst, root = true) =>
new Promise(async (resolve, reject) => {
if (root) {
src = RNFS.DownloadDirectoryPath + '/' + src;
dst = RNFS.DownloadDirectoryPath + '/' + dst;
this.firstSource = src;
}
RNFS.mkdir(dst)
.then(() => {
RNFS.readDir(src)
.then(async (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(async () => {
await this.moveDirectory(filepath, destfile, false);
})
.catch((e) => {
reject(e);
});
} else {
await RNFS.copyFile(filepath, destfile);
}
if (i === files.length - 1) {
RNFS.unlink(this.firstSource)
.then(() => {
resolve();
})
.catch((e) => {
reject(e);
});
}
}
})
.catch((err) => {
console.log(err);
reject(err);
});
})
.catch((err) => {
console.log(err);
reject(err);
});
});
render() {
return <View></View>;
}
}
感谢您的帮助,我用您的解决方案找到了答案
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;
}
}
我在重命名和移动文件夹时遇到问题,因为 react native fs 只有 moveFile 选项只对文件执行而不是 folders.Doing 它递归也很混乱,执行重命名或移动后很难执行行由于同步 execution.I 的选项已附上下面的移动代码 bug.Kindly 帮助我解决这个问题。
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)
})
}
})
提前致谢:)
你可以调用this.moveDirectorySafely("source","destination");
递归move/rename
确保您拥有允许的存储权限
如果你有任何错误,那么你可以调用RNFS.readDir(RNFS.DownloadDirectoryPath).then
来检查这个文件夹是否存在
import React, {Component} from 'react';
import {View} from 'react-native';
var RNFS = require('react-native-fs');
export default class index extends Component {
firstSource = '';
componentDidMount() {
this.moveDirectorySafely('source', 'destination'); //you can call this function
}
moveDirectorySafely = (src, dst) => {
RNFS.exists(dst).then((dirExists) => {
if (dirExists) {
RNFS.unlink(dst).then(() => {
this.moveDirectory(src, dst);
});
} else {
this.moveDirectory(src, dst);
}
});
};
moveDirectory = (src, dst, root = true) =>
new Promise(async (resolve, reject) => {
if (root) {
src = RNFS.DownloadDirectoryPath + '/' + src;
dst = RNFS.DownloadDirectoryPath + '/' + dst;
this.firstSource = src;
}
RNFS.mkdir(dst)
.then(() => {
RNFS.readDir(src)
.then(async (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(async () => {
await this.moveDirectory(filepath, destfile, false);
})
.catch((e) => {
reject(e);
});
} else {
await RNFS.copyFile(filepath, destfile);
}
if (i === files.length - 1) {
RNFS.unlink(this.firstSource)
.then(() => {
resolve();
})
.catch((e) => {
reject(e);
});
}
}
})
.catch((err) => {
console.log(err);
reject(err);
});
})
.catch((err) => {
console.log(err);
reject(err);
});
});
render() {
return <View></View>;
}
}
感谢您的帮助,我用您的解决方案找到了答案
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;
}
}