zoukankan      html  css  js  c++  java
  • nodejs 删除空文件

    var fs = require("fs")
    var path = require("path")
    
    var listRealPath = path.resolve(__dirname);
    var newPath = path.resolve(__dirname+'/newFile');
    readDirSync(listRealPath) // 以当前js文件所在目录进行遍历
    function readDirSync(filePath) {
    	var pa = fs.readdirSync(filePath);
    	console.log(pa);
    	pa.forEach(function(ele, index) {
    		var info = fs.statSync(filePath + "/" + ele)
    		if (info.isDirectory()) {
    			// 如果是空文件夹 则执行删除操作
    			if(isEmptyDir(filePath + "/" + ele)){
    				console.log('delete foder=> ' + ele);
    				fs.rmdirSync(filePath + "/" + ele);
    			}
    		} else {
    			console.log('不是空文件夹不执行删除操作===>');
    		}
    	})
    }
    
    // 判断是否是空文件夹
    function isEmptyDir(fPath){
    	var pa = fs.readdirSync(fPath);
    	if(pa.length === 0){
    		return true;
    	} else {
    		return false;
    	}
    	
    }
    

      

    使用方式

    将此js文件拖放到需要批量删除的文件夹目录列表中

    执行(xxxx是你命名的js名字)

    node xxxx.js
    

      

  • 相关阅读:
    ISO 学习笔记 2015-03-15
    IOS 学习笔记 20150314
    IOS 学习日志 2015-3-13
    Letter Combinations of a Phone Number
    anagrams
    Pow(x, n)
    Distinct Subsequences
    Excel Sheet Column Number
    MIT 三课程
    c++ 重载,覆盖,重定义
  • 原文地址:https://www.cnblogs.com/MainActivity/p/11296511.html
Copyright © 2011-2022 走看看