多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# .statSync ### 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 时 ``` <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.statSync.html>