比如某个数据库下对SQL SERVER的数据库进行了每天的备份,现在要保留7天以内的,其他的删除掉,用ASP可以实现了,但要用到filesystemobject,不大爽
首先写一个js文件clearDatabaseBackup,内容如下
function deleteBackupFile(beforedays) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var dir = fso.GetFolder("D:\\数据库备份"); //改成你的数据库备份目录
for (var fc = new Enumerator(dir.SubFolders); !fc.atEnd(); fc.moveNext()) {
//假设数据文件的备份文件都是以abc_log开头的,比如abc_log_xxxx
if (fc.item().name.substr(0,9) == "abc_log") {
for (var logfiles = new Enumerator(fc.item().Files); !logfiles.atEnd(); logfiles.moveNext()) {
var fileName = logfiles.item().name;
var year = "20" + fileName.substr(19, 2);
var month = fileName.substr(21, 2);
var day = fileName.substr(23, 2);
var days = Math.round(((new Date()).getTime() - Date.UTC(year, month - 1, day)) / 1000 / 60 / 60 / 24);
if (days >= beforedays) logfiles.item().Delete();
}
}
}
}
//保存一周内的数据库备份文件
deleteBackupFile(7);
然后写一个BAT文件,加入到任务计划
cscript clearDatabaseBackup.js