上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

使用node批量删除目录,无论多少层级,封装了一下

[复制链接]
  • TA的每日心情
    无聊
    2023-11-2 17:37
  • 签到天数: 275 天

    [LV.8]以坛为家I

    114

    主题

    453

    回帖

    974

    积分

    荣誉开发者

    积分
    974

    荣誉开发者油中2周年卓越贡献生态建设者油中3周年

    发表于 2024-4-26 16:48:48 | 显示全部楼层 | 阅读模式
    const fs = require('fs');
    const path = require('path');
    
    function deleteGitFolders(directory, depth, deleteDirName) {
      if (depth === 0) {
        return;
      }
    
      fs.readdir(directory, (err, files) => {
        if (err) {
          console.error('Error reading directory:', err);
          return;
        }
        console.log(files)
        files.forEach(file => {
          const filePath = path.join(directory, file);
          if (fs.statSync(filePath).isDirectory()) {
            if (file === deleteDirName) {
              console.log('Deleting .git directory:', filePath);
              fs.rmdir(filePath, { recursive: true }, err => {
                if (err) {
                  console.error('Error deleting .git directory:', err);
                } else {
                  console.log('.git directory deleted successfully:', filePath);
                }
              });
            } else {
              deleteGitFolders(filePath, depth - 1); // 递归调用,深度减一
            }
          }
        });
      });
    }
    
    const startDirectory = './';
    deleteGitFolders(startDirectory, 2, '.git'); // 限制递归深度为2
    

    如果你要使用只需要deleteGitFolders(startDirectory, 2, '.git'); 改成你指定的目录,你要递归的层级,你要删除的目录名

    直接无限层级递归

    const fs = require('fs');
    const path = require('path');
    
    function deleteGitFolders(directory) {
      fs.readdir(directory, (err, files) => {
        if (err) {
          console.error('Error reading directory:', err);
          return;
        }
    
        files.forEach(file => {
            console.log(file)
          const filePath = path.join(directory, file);
          fs.stat(filePath, (err, stats) => {
            if (err) {
              console.error('Error stating file:', err);
              return;
            }
    
            if (stats.isDirectory()) {
              if (file === '.git') {
                console.log('Deleting .git directory:', filePath);
                fs.rmdir(filePath, { recursive: true }, err => {
                  if (err) {
                    console.error('Error deleting .git directory:', err);
                  } else {
                    console.log('.git directory deleted successfully:', filePath);
                  }
                });
              } else {
                deleteGitFolders(filePath); // 递归调用,进入下一级目录
              }
            }
          });
        });
      });
    }
    
    const startDirectory = './';
    deleteGitFolders(startDirectory);
    
    接脚本定制
    I frequently record, because want to leave something.
  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    638

    主题

    5229

    回帖

    6102

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6102

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2024-4-26 17:21:16 | 显示全部楼层
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    638

    主题

    5229

    回帖

    6102

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6102

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2024-4-26 17:22:01 | 显示全部楼层
    懂了
    删除特定层级文件...
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表