Stats|Object FileSystemManager.statSync(string path, boolean recursive)

FileSystemManager.stat 的同步版本

参数

string path

文件/目录路径

boolean recursive
支持版本 >= 2.3.0

是否递归获取目录下的每个文件的 Stats 信息

返回值

Stats|Object stats

当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。

错误

错误码 错误信息 说明
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.statSync.html