javascript - 在 Node 中查找包含文件名的字符串的目录部分?
问题描述
使用 Node 处理一堆文件,我需要将文件名与目录分开。节点是否有一种简单的方法可以做到这一点而无需额外的依赖?我可以使用像Filename Regex这样的 NPM 包,但我想我会检查是否有现成可用的东西?
例如,假设我们有src/main/css/file.css
. 希望这样的事情是可能的:
const fs = require('fs');
const path = fs.filePath(String pathAndFileName); //path = src/main/css
const file = fs.fileName(String pathAndFileName); //file = file.css
解决方案
用于操作文件路径的实用程序位于路径模块中。 https://nodejs.org/api/path.html
const {dirname, basename} = require('path');
const path = dirname(String pathAndFileName);
const file = basename(String pathAndFileName);
推荐阅读
- android - 如何仅根据最低位置精度更新位置
- android - Android WebViewClient 中的 onReceivedClientCertRequest 方法只调用一次
- javascript - 在迭代期间忽略数组中的属性
- assembly - MIPS 我如何处理前一个 ALU 指令上的分支而不停止?
- ios - Xamarin.ios:应用程序中未加载图像资产目录
- python - 如何正确地将线放在 seaborn 箱线图的顶部?
- php - 从 url 获取两个字段并将它们转换为变量以查询搜索
- javascript - 如何从表格单元格中获取值
- bash - 如何使用 Docker 获取入口点脚本?
- android - 在我们上线 android 后,使用 Paypal sdk 的付款不起作用