I currently have a script that deletes old log files and then takes any files newer than the specified time frame and zips them up into a zip with the name of the localhost.zip.
What I am trying to do is just add the Date when the files were zipped appended onto the name of the zip.
So something ike localhost_Date.zip.
I know that I need to add something after “%computername%” but I am just not sure of the syntax to deal with it.
Thanks for any help in advance. Also if you see any improvements that could be made to my script just let me know. It’s pretty much just a beginners workings with tweaking other scripts found online to suit my needs.
Option Explicit
Dim oFSO, oFolder, sDirectoryPath
Dim oFileCollection, oFile, sDir
Dim iDaysOld
' Specify Directory Path From Where You want to clear the old files
sDirectoryPath = "C:\Testscripts\testfolder\"
' Specify Number of Days Old File to Delete
iDaysOld = 7
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files
For each oFile in oFileCollection
'Specify the Extension of file that you want to delete
'and the number with Number of character in the file extension
If LCase(Right(Cstr(oFile.Name), 3)) = "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")
PathLogs = "C:\Testscripts\testfolder\" 'This path just has some test logs
' Loop through the logs and zip and move each file (if required, you could just move files with an '.log' extension)
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%" & ".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:\Testscripts\testfolder\*.zip" , "C:\Testscripts\testfolder\Archived"
WScript.Echo "Move Successful."
I skipped the gory details, sorry. Here is a two-line VBScript code to format current date as
yyyy-mm-dd(keeping in mind that/is not allowed in filenames):Apart from that, this line:
Can be written as using FSO.GetExtensionName: