# FileSystemManager.statSync

更新时间:2024-07-23 11:06:59

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

FileSystemManager.stat 的同步版本

# 参数

# string path

文件/目录路径

# boolean recursive

是否递归获取目录下的每个文件的 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 时

let fs = ks.getFileSystemManager()  
fs.stat({  
  path: `${ks.env.USER_DATA_PATH}/testDir`,  
  success: res => {  
    console.log(res.stats.isDirectory())  
  }  
})  

recursive 为 true 时

fs.stat({  
  path: `${ks.env.USER_DATA_PATH}/testDir`,  
  recursive: true,  
  success: res => {  
    Object.keys(res.stats).forEach(path => {  
      let stats = res.stats[path]  
      console.log(path, stats.isDirectory())  
    })  
  }  
})  
Copyright ©2025, All Rights Reserved