FileSystemManager.rename
收藏
我的收藏

基础库 1.15.0 开始支持本方法,这是一个异步方法。

重命名文件,可以把文件从 oldPath 移动到 newPath。

前提条件
业务背景
使用限制
注意事项
因为开发者只有在 ttfile://user 目录下才有写的权限, 因此 rename 只能在用户目录下进行。
支持沙盒
相关教程

语法

FileSystemManager.rename(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
oldPathstring
源文件路径,可以是普通文件或目录,必须以 `ttfile://user` 开头
1.15.0
newPathstring
新文件路径,必须以 `ttfile://user` 开头
1.15.0
successfunction
接口调用成功的回调函数
1.15.0
failfunction
接口调用失败的回调函数
1.15.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.15.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"rename:ok"
1.15.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"rename:fail " + 详细错误信息
1.15.0

错误码

errNoerrMsg说明最低支持版本
20000rename:fail oldPath is invalid
oldPath 参数错误
1.99.0
20000rename:fail newPath is invalid
newPath 参数错误
1.99.0
21101rename:fail newPath is invalid
无操作权限(源路径不可读/目标路径不可写)
1.99.0
21102rename:fail no such file or directory, rename %s -> %s
源文件不存在或目标文件的父级目录不存在
1.99.0
21103rename:fail operation not permitted, rename %s
path 类型不一致
1.99.0
21104rename:fail user dir saved file size limit exceeded
超过 User 目录大小的限制
1.99.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

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(/(\..+)?$/, "")

  fileSystemManager.rename({
    oldPath: fileItem.filePath,
    newPath,
    success(_res) {
      console.log("重命名成功")
    },
    fail(res) {
      console.log("重命名失败", res.errMsg)
    },
  })
}