?
比如百度網盤某目錄下存有如下文件:
要求:將如上圖文件目錄下的文件,每個月只保留最后(新)一個(根據文件名中包含的日期),其它刪除。
比如7月份有3個文件,只保留2019-07-21那天的文件,刪除7月份的其它2個。
?
代碼實現步驟:
1、獲取當前目錄名稱:“CZSX030A”
2、遍歷獲取每個文件名中包含的日期,如“CZSX030A_20190721000002.bak”-->"201907"
3、模擬點擊事件,選中每個月份中除了第一個文件
var temptime = "",sltCount = 0;
var currentPath= $(".FuIxtL li:last-child span:last-child").attr("title");
var tag = currentPath.substring(currentPath.lastIndexOf('/')+1);//當前目錄名稱
$("dd").each(function() {var tt =$(this).find(".file-name>.text").text().replace(tag+"_","").replace("-", "").substring(0, 6);//截取文件名中的日期//var temptag =$(this).find(".file-name>.text").text().replace(tag+"_","").replace("-", "").substring(1, 9); if (tt != "" && !isNaN(tt)) {if (tt != temptime) {temptime = tt;} else {$(this).find(".NbKJexb").trigger('click');//選擇日期相同且非第一個的記錄sltCount++;}console.log(tt);}
});console.clear();
console.log(tag + " 共選擇了"+sltCount+"條數據");
?
4、模擬點擊“刪除”按鈕
?
$("a[title='刪除']:visible").trigger('click');
5、模擬點擊彈出提示層“確認”按鈕
?
$(".g-button-blue-large").trigger('click');
6、等刪除完成
?
7、執行結果
?附:完整代碼
本文代碼的功能是根據月份清理文件,當然可以修改條件代碼,執行您想執行的操作,比如可以刪除目錄下的重復文件等。
/**
*清理網盤文件JS代碼
*Jackie
*2019.07.10
**/
var temptime = "",sltCount = 0;
var currentPath= $(".FuIxtL li:last-child span:last-child").attr("title");
var tag = currentPath.substring(currentPath.lastIndexOf('/')+1);//當前目錄名稱
$("dd").each(function() {var tt =$(this).find(".file-name>.text").text().replace(tag+"_","").replace("-", "").substring(0, 6);//截取文件名中的日期//var temptag =$(this).find(".file-name>.text").text().replace(tag+"_","").replace("-", "").substring(1, 9); if (tt != "" && !isNaN(tt)) {if (tt != temptime) {temptime = tt;} else {$(this).find(".NbKJexb").trigger('click');//選擇日期相同且非第一個的記錄sltCount++;}console.log(tt);}
});console.clear();
console.log(tag + " 共選擇了"+sltCount+"條數據");
if(sltCount>0)
{setTimeout(function(){console.log("執行刪除!");$("a[title='刪除']:visible").trigger('click');setTimeout(function(){console.log("確認刪除!");$(".g-button-blue-large").trigger('click');},1000);},1000);
}
else
{console.log("無可刪除記錄!");
}
//返回
setTimeout(function(){window.history.back(-1);
},2000);
//自動定位到上次操作目錄,便于處理下一目錄
var currentDD;
var container=$('.NHcGw');
$("dd").each(function() {if($(this).find("a:eq(0)").text()==tag){currentDD=$(this); }
});if(currentDD)
{if(currentDD.offset().top>container.offset().top){currentDD.css("background-color","yellow");//自動定位到上次操作目錄container.animate({scrollTop: currentDD.offset().top-container.offset().top + "px"},500);}//自動打開下一目錄currentDD.next().find("a:eq(0)").trigger('click');
}
console.clear();
?