vscode 插件开发怎么判断一个目录下是否存在onnx类型文件
时间: 2024-04-30 07:18:12 浏览: 17
在 VSCode 插件开发中,可以使用 Node.js 提供的 `fs` 模块来判断一个目录下是否存在某种类型的文件。具体实现如下:
```javascript
const fs = require('fs');
const path = require('path');
const directoryPath = '/path/to/directory';
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error(`Unable to scan directory: ${err}`);
return;
}
const onnxFiles = files.filter(file => path.extname(file) === '.onnx');
if (onnxFiles.length > 0) {
console.log(`Directory contains ${onnxFiles.length} ONNX files`);
} else {
console.log('Directory does not contain any ONNX files');
}
});
```
上述代码首先使用 `fs.readdir()` 方法读取指定目录中的所有文件,然后使用 `path.extname()` 方法获取文件扩展名,最后使用 `Array.filter()` 方法过滤出扩展名为 `.onnx` 的文件。如果有符合条件的文件,则输出文件数量;如果没有,则输出提示信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)