多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# .stat ### FileSystemManager.stat(Object object) 获取文件 Stats 对象 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本pathstring是文件/目录路径recursivebooleanfalse否是否递归获取目录下的每个文件的 Stats 信息>= 2.3.0successfunction否接口调用成功的回调函数failfunction否接口调用失败的回调函数completefunction否接口调用结束的回调函数(调用成功、失败都会执行)###### object.success 回调函数 **参数** **Object res** 属性类型说明支持版本stats[Stats](/read/wechat-game-api/194.md)/Object当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。###### object.fail 回调函数 **参数** **Object res** 属性类型说明支持版本errMsgstring错误信息**res.errMsg 的合法值** 值说明fail permission denied, open ${path}指定的 path 路径没有读权限fail no such file or directory ${path}文件不存在#### 示例代码 recursive 为 false 时 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">let fs </span><span class="pun">=</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">getFileSystemManager</span><span class="pun">()</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">fs</span><span class="pun">.</span><span class="pln">stat</span><span class="pun">({</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> path</span><span class="pun">:</span><span class="pln"> </span><span class="pun">`</span><span class="pln">$ </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">env</span><span class="pun">.</span><span class="pln">USER_DATA_PATH</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">/</span><span class="pln">testDir</span><span class="pun">`,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="pln">res</span></span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">stats</span><span class="pun">.</span><span class="pln">isDirectory</span><span class="pun">())</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` recursive 为 true 时 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">fs</span><span class="pun">.</span><span class="pln">stat</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> path</span><span class="pun">:</span><span class="pln"> </span><span class="pun">`</span><span class="pln">$ </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">env</span><span class="pun">.</span><span class="pln">USER_DATA_PATH</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">/</span><span class="pln">testDir</span><span class="pun">`,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> recursive</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="pln">res</span></span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="typ">Object</span><span class="pun">.</span><span class="pln">keys</span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">stats</span><span class="pun">).</span><span class="pln">forEach</span><span class="pun">(</span><span class="hljs-params"><span class="pln">path</span></span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> let stats </span><span class="pun">=</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">stats</span><span class="pun">[</span><span class="pln">path</span><span class="pun">]</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">path</span><span class="pun">,</span><span class="pln"> stats</span><span class="pun">.</span><span class="pln">isDirectory</span><span class="pun">())</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/file/FileSystemManager.stat.html>