FileSystemManager.stat(Object object)

获取文件 Stats 对象

参数

Object object
属性 类型 默认值 是否必填 说明 支持版本
path string 文件/目录路径
recursive boolean false 是否递归获取目录下的每个文件的 Stats 信息 >= 2.3.0
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数

参数

Object res

属性 类型 说明 支持版本
stats Stats/Object 当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。
object.fail 回调函数

参数

Object res

属性 类型 说明 支持版本
errMsg string 错误信息

res.errMsg 的合法值

说明
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在

示例代码

recursive 为 false 时

  1. let fs = wx.getFileSystemManager()
  2. fs.stat({
  3. path: `$ {
  4. wx.env.USER_DATA_PATH
  5. }
  6. /testDir`,
  7. success: res => {
  8. console.log(res.stats.isDirectory())
  9. }
  10. })

recursive 为 true 时

  1. fs.stat({
  2. path: `$ {
  3. wx.env.USER_DATA_PATH
  4. }
  5. /testDir`,
  6. recursive: true,
  7. success: res => {
  8. Object.keys(res.stats).forEach(path => {
  9. let stats = res.stats[path]
  10. console.log(path, stats.isDirectory())
  11. })
  12. }
  13. })

原文: https://developers.weixin.qq.com/minigame/dev/api/file/FileSystemManager.stat.html