Answers:
我四处张望,发现了一种Powershell方式:
从指定的文件夹中删除超过8天的所有文件(带预览)
dir |? {$_.CreationTime -lt (get-date).AddDays(-8)} | del -whatif
(删除-whatif使其实现)
喜欢Jeff的PowerShell命令,但是对于没有PowerShell的Windows计算机的替代vbs解决方案,您可以尝试以下操作。
另存为<filename>.vbs
并执行:
<filename>.vbs <target_dir> <NoDaysSinceModified> [Action]
第三个参数[Action]
是可选的。没有它,文件将比<NoDaysSinceModified>
列出的文件早。设置为D
它将删除早于<NoDaysSinceModified>
例
PurgeOldFiles.vbs "c:\Log Files" 8
将列出所有c:\Log Files
早于8天的文件
PurgeOldFiles.vbs "c:\Log Files" 8 D
将删除c:\Log Files
8天之前的所有文件
注意:这是SQLServerCentral.com上Haidong Ji脚本的修改版本
Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
Dim fAction
sDirectoryPath = WScript.Arguments.Item(0)
iDaysOld = WScript.Arguments.Item(1)
fAction = WScript.Arguments.Item(2)
Set oFSO = CreateObject("Scripting.FileSystemObject")
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
If UCase(fAction) = "D" Then
'Walk through each file in this folder collection.
'If it is older than iDaysOld, then delete it.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
else
'Displays Each file in the dir older than iDaysOld
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
Wscript.Echo oFile.Name & " " & oFile.DateLastModified
End If
Next
End If
'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
Set fAction = Nothing
看看这个http://sourceforge.net/projects/delold,因为这是我使用的。
简单但有效。delold -d 14删除当前文件夹中超过 14天的文件。
我以前在powershell之前使用过的另一种选择:
http://lifehacker.com/133190/geek-to-live--hard-drive-janitor