我的脚本实际上没有生成zip文件的问题。当我使用如下所示的路径测试脚本时:
C:\ Users \用户鲍勃\桌面\ Folder1中\ FOLDER2
如果文件夹1中的测试日志文件超过7天被删除,并且被压缩并移动到文件夹2,它将完全正常运行。它将生成包含所有日志文件的压缩文件,并具有正确的命名集。
所以我知道至少脚本的逻辑适用于此。
我的问题是我需要这个脚本来浏览计算机上的安全日志并删除任何超过7天的日志,然后压缩任何剩下的并发送到已安装的共享驱动器。当我将路径更改为:
C:\ Windows \ System32 \ Config(日志所在的位置)
它仍将删除超过7天的任何日志文件,但它不会生成任何剩余的zip文件。即使脚本没有产生错误,它也什么都不做。我一直试图解决这个问题,但我的代码没有运气。如果有人能看看我所拥有的东西,让我知道我哪里误入歧途会非常有帮助。
提前谢谢,脚本在下面找到。
'先读 “------------------------------------------------- -----------------------------------------
'第14-53行删除任何超过7天的日志文件。更改“iDaysOld =”中的值将更改保留或删除文件的时间范围。 '如果不需要删除文件,可以取出脚本的这部分,存档/移动功能仍然完好
'第57-102行包含压缩日志文件并将其发送到新文件夹的功能。压缩文件以localhost命名,日期/时间戳附加到文件名。
“---------------------------------------------- --------------------------------------------
Option Explicit
Dim oFSO, oFolder, sDirectoryPath
Dim oFileCollection, oFile, sDir
Dim iDaysOld
'从您要清除旧文件的位置指定目录路径
sDirectoryPath = "C:\Windows\System32\config"
'指定要删除的旧文件的天数
iDaysOld = 7
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files
For each oFile in oFileCollection
'指定要删除的文件的扩展名 '和文件扩展名中的字符数
的数字If LCase(oFSO.GetExtensionName(oFile.Name)) = "log" Then
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
End If
Next
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
WScript.Echo "Press to start zipping log files."
Dim objFile, objPath, objFolder, Command, PathLogs, RetVal
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell = CreateObject("WScript.Shell")
Dim d : d = Date()
Dim dateStr : dateStr = Year(d) & "-" & Right("00" & Month(d), 2) & "-" & Right("00" & Day(d), 2)
Dim t : t = Time()
Dim timeStr: timeStr = Hour(t) & "-" & Right("00" & Minute(t), 2) & "-" & Right("00" & Second(t), 2)
'日志所在的路径
PathLogs = "C:\Windows\System32\config"
'循环浏览日志并压缩
Set objPath = objFSO.GetFolder(PathLogs)
For Each objFile In objPath.Files
If (LCase(objFSO.GetExtensionName(objFile)) = "log") Then
' zip files
Command = """C:\Program Files\7-zip\7z.exe"" a " & PathLogs & "%computername%" & "-" & dateStr & "-" & timeStr & ".zip " & PathLogs & objFile.Name
RetVal = objShell.Run(Command,0,true)
End If
Next
WScript.Echo "Zip Successful."
WScript.Echo "Now Moving Zipped Files into Archived Folder"
'move files
Set objFSO = CreateObject("Scripting.FilesystemObject")
objFSO.MoveFile "C:\Windows\System32\config\*.zip" , "C:\Testscripts\testfolder\Archived"
WScript.Echo "Move Successful."
答案 0 :(得分:0)
我可能会尝试回显7zip命令行,检查它是否正确并从脚本运行的同一位置手动运行它。当你看到它时可能看起来不对,或者7zip可能会给你一条消息来指示发生了什么。