- API 概览
- 开放能力
- 基础
- 渲染
- 设备
- 文件
- 位置
- 媒体
- 网络
- 游戏转发分享
- 实时语音
- 数据缓存
- 系统
- 广告
- 界面
- 支付
- Worker
FileSystemManager.renameSync收藏我的收藏
收藏
我的收藏基础库 1.15.0 开始支持本方法,这是一个同步方法。
重命名文件,可以把文件从 oldPath 移动到 newPath。
前提条件 | 无 |
业务背景 | 无 |
使用限制 | 无 |
注意事项 | 因为开发者只有在 ttfile://user 目录下才有写的权限, 因此 rename 只能在用户目录下进行。 |
相关教程 | 无 |
语法
FileSystemManager.renameSync(oldPath, newPath)
参数说明
oldPath
类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|
string | 是 | 必须以 ttfile://user 开头 源文件路径,可以是普通文件或目录 | 1.15.0 |
newPath
类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|
string | 是 | string 类型, 必须以 ttfile://user 开头 新 文件路径 | 1.15.0 |
返回值
无
错误码
errNo | errMsg | 说明 | 最低支持版本 |
---|---|---|---|
20000 | renameSync:fail oldPath is invalid | oldPath 参数错误 | 1.99.0 |
20000 | renameSync:fail newPath is invalid | newPath 参数错误 | 1.99.0 |
20001 | renameSync:fail param should be xxx, but got xxx | 参数校验错误 | 1.99.0 |
21101 | renameSync:fail newPath is invalid | 无操作权限(源路径不可读/目标路径不可写) | 1.99.0 |
21102 | renameSync:fail no such file or directory, renameSync {oldPath_value} -> {newPath_value} | 源文件不存在或目标文件的父级目录不存在 | 1.99.0 |
21103 | renameSync:fail operation not permitted, renameSync {oldPath_value} | 类型错误(类型不一致) | 1.99.0 |
21104 | renameSync:fail user dir saved file size limit exceeded | 大小超出限制 | 1.99.0 |
代码示例
const fileSystemManager = tt.getFileSystemManager();
fileSystemManager.getSavedFileList({
success(res) {
res.fileList.forEach(removeExt);
},
fail(res) {
console.log("获取失败", res.errMsg);
},
});
/**
* @param {*} fileItem
* @param {string} fileItem.filePath
* @param {number} fileItem.createTime
* @param {number} fileItem.size
*/
function removeExt(fileItem) {
console.log(`移除 ${fileItem.filePath} 的 ext`);
const newPath = fileItem.filePath.replace(/(\..+)?$/, "");
try {
fileSystemManager.renameSync(fileItem.filePath, newPath);
console.log("重命名成功");
} catch (err) {
console.log("重命名失败", err);
}
}
该文档是否有帮助?
点击纠错