Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner VBscript: script that zips log files
    text
    copied!<p>I am having a problem with my script not actually producing a zip file. When I test the script with the paths set to something like this:</p> <p>C:\Users\Bob\Desktop\Folder1\Folder2</p> <p>with the test log files in folder 1 being deleted if older than 7 days, and being zipped and moved to folder 2, it will run perfectly fine. It will produce the zipped file with all of the log files in it and have the proper naming set.</p> <p>So I know that at least the logic of the script works for that.</p> <p>My problem is that I need this script to go through the security logs on a machine and delete any older than 7 days, and then zip up any that are left and be sent to a mounted shared drive. When I change the path to something like:</p> <p>C:\Windows\System32\Config (where the logs are located)</p> <p>it will still delete any log files older than 7 days, but it does not produce a zip file with any that are left. It just does nothing even though the script produces no errors. I've been trying to figure this out with no luck going over my code. If anyone could take a look over what I've had and let me know where I've gone astray that would be extremely helpful.</p> <p>Thank you in advance, the script is found below. </p> <p>'READ FIRST '------------------------------------------------------------------------------------------</p> <p>'Lines 14-53 delete any log files older than 7 days. Changing the value in "iDaysOld =" will change the time frame in which files are kept or deleted. 'If files do not need to be deleted this part of the script can be taken out and the Archive/Move ability will still be intact</p> <p>'Lines 57-102 contain the ability to Zip your log files and send them to a new folder. The zipped file is named after the localhost and a date/timestamp is appended to the file name.</p> <p>'------------------------------------------------------------------------------------------</p> <pre><code>Option Explicit Dim oFSO, oFolder, sDirectoryPath Dim oFileCollection, oFile, sDir Dim iDaysOld </code></pre> <p>' Specify Directory Path From Where You want to clear the old files </p> <pre><code>sDirectoryPath = "C:\Windows\System32\config" </code></pre> <p>' Specify Number of Days Old File to Delete</p> <pre><code>iDaysOld = 7 Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(sDirectoryPath) Set oFileCollection = oFolder.Files For each oFile in oFileCollection </code></pre> <p>'Specify the Extension of file that you want to delete 'and the number with Number of character in the file extension </p> <pre><code>If LCase(oFSO.GetExtensionName(oFile.Name)) = "log" Then If oFile.DateLastModified &lt; (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) &amp; "-" &amp; Right("00" &amp; Month(d), 2) &amp; "-" &amp; Right("00" &amp; Day(d), 2) Dim t : t = Time() Dim timeStr: timeStr = Hour(t) &amp; "-" &amp; Right("00" &amp; Minute(t), 2) &amp; "-" &amp; Right("00" &amp; Second(t), 2) </code></pre> <p>'Path where logs are located</p> <pre><code>PathLogs = "C:\Windows\System32\config" </code></pre> <p>'Loop through the logs and zip </p> <pre><code>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 " &amp; PathLogs &amp; "%computername%" &amp; "-" &amp; dateStr &amp; "-" &amp; timeStr &amp; ".zip " &amp; PathLogs &amp; 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." </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload