使用nodejs删除指定的文件,修改指定文件的名称

// 为什么会有这个东西,因为用百度云下载了很多视频,不知道为什么存在很多 .downloading的文件,所以就有了这段代码,还有就是文件名带了很多没用的信息,想把无用信息剔除掉就有了后面的一段代码
/*
* * 删除目录下 指定 文件方法 * 参数: dir 文件夹名称 * fs.stat => 判断是文件还是文件夹 * fs.unlink => 删除文件 * fs.readdir => 读取文件夹内容 */ const fs = require('fs') const path = require('path') const deleteFiles = function (dir) { fs.readdir(dir, function (err, files) { files.forEach(function (filename) { var src = path.join(dir, filename) fs.stat(src, function (err, st) { if (err) { throw err } // 判断是否为文件 if (st.isFile()) { // 这里可以使用正则,也可以使用其他方法,比如字符串处理等,/\.d\.ts$/ if (/\.we$/.test(filename)) { fs.unlink(src, err => { if (err) throw err console.log('成功删除:' + src) }) } } else { // 递归文件夹 deleteFiles(src) } }) }) }) } deleteFiles('./')

 

修改文件名称

fs = require('fs') // 引用文件系统模块

const PATH = `./src/` // 当前文件夹

const readFileList = function (path, filesList) {
    filesList = filesList || []
    let files = fs.readdirSync(path)
    files.forEach(function (filename, index) {
      // const stat = fs.statSync(path + filename); //读取的文件信息

      // isDirectory 判断是不是目录
      if (fs.statSync(path + filename).isDirectory()) {
        // 递归读取文件
        readFileList(`${path}${filename}/`, filesList)
      } else {
        filesList.push({
          path, // 路径
          filename // 名字
        })
      }
    })
    return filesList
}
// 修改文件名称
const rename = function (oldPath, newPath, filename, newSuffixFile) {
    fs.rename(oldPath, newPath, function (err) {
      if (err) {
        throw err
      }
      console.log(`${filename} 修改为 => ${newSuffixFile}`)
    })
}
// 批量修改文件名称
const  getChangeFiles = function (path, oldSuffix, newSuffix) {
    if (!oldSuffix && !newSuffix) {
      console.log(`后缀未设置`)
    }
    this.readFileList(path).forEach(item => {
      if (item.filename.indexOf(oldSuffix) > -1) {
        console.log(item.filename)
        let oldPath = item.path + item.filename,
          newSuffixFile = item.filename.split(oldSuffix)[0] + newSuffix,
          newPath = item.path + newSuffixFile
        rename(oldPath, newPath, item.filename, newSuffixFile)
      }
    })
}

getChangeFiles(PATH, `.we`, `.js`)


// 引入fs文件处理模块
const fs = require('fs')
// 现在我们要关心的是‘icons‘文件夹
// 我们不妨用变量表示这个文件夹名称,方便日后维护和管理
const src = 'dist'

// API文档中中找到遍历文件夹的API
// 找到了,是fs.readdir(path, callback)
// 文档中有叙述:
// 读取 path 路径所在目录的内容。 回调函数 (callback) 接受两个参数 (err, files) 其中 files 是一个存储目录中所包含的文件名称的数组
// 因此:
fs.readdir(src, function (err, files) {
// files是名称数组,因此
// 可以使用forEach遍历哈, 此处为ES5 JS一点知识
// 如果不清楚,也可以使用for循环哈
files.forEach(function (filename) {
// 下面就是文件名称重命名
// API文档中找到重命名的API,如下
// fs.rename(oldPath, newPath, callback)
// 下面,我们就可以依葫芦画瓢,确定新旧文件名称:
const oldPath = src + '/' + filename
// newPath = src + ‘/‘ + filename.replace(/_/g, ‘-‘);
const newPath = src + '/' + 'index.html'
if (filename === 'Homepad.html') {
// 重命名走起
fs.rename(oldPath, newPath, function (err) {
if (!err) {
console.log(filename + '重命名成功!')
}
})
}
})
})


 

posted @ 2022-04-06 10:50  夏目友人喵  阅读(685)  评论(0编辑  收藏  举报