我想按名称而不是按扩展名从nodejs的存储中删除图像文件。例如:如果我有1.png,我想将其删除。还有1.jpg。我该怎么办?
假设您的存储是本地文件系统,则可以使用@ wrote / read-dir-structure包来递归获取所有文件的列表:
import { unlinkSync } from 'fs'
import readDirStructure, { getFiles } from '@wrote/read-dir-structure'
(async () => {
const path = 'example/directory'
const res = await readDirStructure(path)
const files = getFiles(res.content, path)
const filtered = files.filter((file) => {
return file.startsWith(FILE_NAME)
})
filtered.forEach((file) => unlinkSync(file))
})()