我有一个用VBScript编写的简单清除脚本,用于比较指定文件的创建日期和今天的日期之间的差异,如果日期大于特定天数,则应将其删除。
我还提供了一种日志记录机制,它将日期和已删除的文件写入文本文件。
我使用wscript命令在命令提示符下(以管理员身份)运行脚本。我也试过过cscript。
问题:生成日志文件并包含标题时,文件本身不会被删除,因此日志中没有删除数据。我对VBScript并不十分熟悉,但代码相当直观。经过无数次调整和尝试修改之后,这种糟糕的事情仍然不会起到应有的作用!
On Error Resume Next
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\log.txt")
sRoot = "C:\file_folder" 'Path root to look for files
today = Date
nMaxFileAge = 7 'Files older than this (in days) will be deleted
DeleteFiles(sRoot)
Function DeleteFiles(ByVal sFolder)
Set oFolder = oFileSys.GetFolder(sFolder)
Set aFiles = oFolder.Files
Set aSubFolders = oFolder.SubFolders
objLog.WriteLine "Purging Log"
objLog.WriteLine "Dated " & today
For Each file in aFiles
dFileCreated = FormatDateTime(file.DateCreated, "2")
If DateDiff("d", dFileCreated, today) > nMaxFileAge Then
file.Delete(True)
objLog.WriteLine "The file named '" & file & "' was successfully deleted."
End If
Next
For Each folder in aSubFolders
DeleteFiles(folder.Path)
Next
End Function