問題來源:
問題內容:
我有一個資料夾,會備份每天的資料,這個資料夾裡面有每天的子資料夾,格式如下:
備份資料夾=>0101、0102、0103…1231,裡面裝著每天的備份資料,但我想做一個批次檔來刪除三個月前的資料,例如:
今天9月21,我要刪掉6月21以前的備份,但我子資料夾必須留著,我要刪的只有裡面的檔案,請問各位大大,我該如何寫這樣的批次檔呢?
問題答案:
分成兩個部份來回答:
1、DelOld.cmd:
@echo off cscript /nologo ListOldFolder.vbs log > FolderList.txt for /f %%f in (FolderList.txt) do ( rd /s /q "%%f" if not exist "%%f" md "%%f" )
|
2、ListOldFolder.vbs:
' ************************************************************************ ' * Modification history ' * Date Level Author Description ' * ---------- ----- ------------ ------------------------------------ ' * 2009/09/23 P0.00 Eric Su Initial Release ' * ' ************************************************************************ ' Eric Su 的部落格:http://sn.im/e7q0c、http://www.pushba.com/View.aspx?gid=7a0bcaa2 ' ************************************************************************
'Usage : ListOldFolder.vbs log_path keep_mons Const StdOut = 1 Dim objArgs, where, mons Set objArgs = WScript.Arguments
where = "log" mons = 3 '讀取前二個參數,省略時,保留 3 個月資料 If objArgs.Count >= 1 Then where = objArgs(0) End If
If objArgs.Count >= 2 Then mons = objArgs(1) End If
DeleteFilesBymons where, mons Set objArgs = Nothing
Sub DeleteFilesByMons(where, keepMons) Dim fso, folder, sf, f Dim aDay, today
today = Now aDay = DateAdd("m", -keepMons, today) today = Right("00" & Month(today), 2) & Right("00" & Day(today), 2) aDay = Right("00" & Month(aDay), 2) & Right("00" & Day(aDay), 2)
Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(where) Then Set folder = fso.GetFolder(where) Set sf = folder.SubFolders If aDay < today Then For Each f In sf If f.Name <= aDay Or f.Name > today Then Output(f) End If Next Else For Each f In sf If f.Name <= aDay And f.Name > today Then Output(f) End If Next End If End If
Set f = Nothing Set sf = Nothing Set folder = Nothing Set fso = Nothing End Sub
Sub Output(where) Dim fso, sout Set fso = CreateObject("Scripting.FileSystemObject") Set sout = fso.GetStandardStream(StdOut) sout.WriteLine where.Path
Set sout = Nothing Set fso = Nothing End Sub
|
沒有留言:
張貼留言