统计百度网盘文件个数 V3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/admans/article/details/80726558

继续优化


/********************
*百度文件数量统计 V3
*2018.06.18
*Jackie
********************/


/************
*百度查询接口
http://pan.baidu.com/api/list?
dir=				//查询目录
&num=100000			//分页大小 最大支持99999999999999 默认1000
&page=1				//页码
&order=time			//排序属性
&desc=1				//排序顺序
&clienttype=0
&showempty=0
************/



var root = "";//指定目录,空取当前目录
var maxPageSize=99999999999999; //最多支持这么多,超过此值报错
var dskApi="https://pan.baidu.com/api/list?&num="+maxPageSize+"&page=1&dir=";
var totalCount = 0;
var startTime = new Date();
var clearTimes=0;
var asyncType=false;//true 异步,false 同步
var clearMaxTimes=50;//最大显示行数,超过清一屏
function timeSpan(stime, etime) {
	var usedTime = etime - stime;
	var days = Math.floor(usedTime / (24 * 3600 * 1000));
	var leave1 = usedTime % (24 * 3600 * 1000);
	var hours = Math.floor(leave1 / (3600 * 1000));
	var leave2 = leave1 % (3600 * 1000);
	var minutes = Math.floor(leave2 / (60 * 1000));
	var leave3 = leave2 % (60 * 1000);
	var seconds = Math.round(leave3 / 1000);
	var time ="";
	if(days>0)
	{
		time+=days+"天";
	}
	if(hours>0)
	{
		time+=hours+"小时";
	}
	if(minutes>0)
	{
		time+=minutes+"分钟";
	}
	 
	time+=seconds+"秒";
	 
    return time;  
} 

(function($){
    $.getUrlParam = function (name) {
        var search = document.location.hash;
        var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
        var matcher = pattern.exec(search);
        var items = null;
        if (null != matcher) {
            try {
                items = decodeURIComponent(decodeURIComponent(matcher[1]));
            } catch (e) {
                try {
                    items = decodeURIComponent(matcher[1]);
                } catch (e) {
                    items = matcher[1];
                }
            }
        }
		return items;
    }
})(jQuery);

if(root=="")
{
	root=$.getUrlParam("path");
}

function GetFilesCount(fileLists)
{
	var count=0;
	 
	if(fileLists==undefined)
	{
		return 0;
	}
	var ffList=fileLists.filter(function (e) { return e.isdir == 0; });
	count=ffList.length;
	totalCount=totalCount+ffList.length;

	var ddList=fileLists.filter(function (e) { return e.isdir == 1; });
	var dl=ddList.length;
	if(dl>0)
	{
		for (var index=0; index<dl; index++) {  
				 (function(index) {  
						var file=fileLists[index];
						count=count+GetDirFilsCount(file.path);				
					})(index);  
				   
		 }  
	}
	return count;
}

function GetDirFilsCount(dirName)
{
	var dfCount=0;
	$.ajax({
		url: dskApi+''+dirName,
		type: 'get',
		async: asyncType,
		timeout: 3000,          // 设置超时时
		success: function(data) {
			var fileLists = data.list;
			dfCount=GetFilesCount(fileLists);	

			clearTimes++;	
			if(clearTimes==clearMaxTimes)
			{
				console.clear();
				clearTimes=0;
			}
			//console.count();
			console.log("→"+decodeURIComponent(dirName)+":"+dfCount);
			console.log("☋统计目录为:“"+decodeURIComponent(root)+"” 当前计算总数:"+totalCount+" 用时:" +timeSpan(startTime, new Date()));
	}
	});
	return dfCount;
}


GetDirFilsCount(root);


猜你喜欢

转载自blog.csdn.net/admans/article/details/80726558