Solved

Need help explaining these commands for VBS Script that clears IIS logs

Posted on 2015-02-17
4
78 Views
Last Modified: 2016-06-19
I am looking to compress the files in the W3SVC1 - W3SVC7 folders every 30 days and then delete those compressed folders when they reach 1 year old.

Server:  2008 R2
Path: C:\inetpub\logs\LogFiles\W3SVC1\ ALL THE TEXT FILES IN HERE

The solution I took from BRANDONMAYER on another question asked about this seems to be what I need except for the 30 days needs to be changed to 365.

I just want to make sure I don't destroy/break anything while attempting on a production server.

Would this work for compressing every 30 days and deleting after 1 year? I am not fluent in VBS.  Also, I would be able to set this to run automatically with Windows scheduler to run every 30 days?


TAKEN FROM BRANDONMAYER
------------------
Date.vbs

output = 2
mdays = 0
On Error Resume Next
output = WScript.Arguments.Item(0)
mdays = WScript.Arguments.Item(1)  
On Error Goto 0

If output >= 2 Then
      strSortDate = Right(DatePart("yyyy",Date-365),2)
Else
      strSortDate = DatePart("yyyy",Date-mdays)
End If

If DatePart("m",Date-mdays) < 10 Then
      strSortDate = strSortDate & "0"
End If
strSortDate = strSortDate & DatePart("m",Date-mdays)

If output >= 1 Then
      If DatePart("d",Date-mdays) < 10 Then
            strSortDate = strSortDate & "0"
      End If
      strSortDate = strSortDate & DatePart("d",Date-mdays)
End If

Wscript.Echo(strSortDate)
----------------------------------------------------
cleanuplogs.cmd

@FOR /F "tokens=*" %%i in ('cscript.exe <pathtodate.vbs>\date.vbs 0 365') do @SET date1=%%i
@FOR /F "tokens=*" %%i in ('cscript.exe <pathtodate.vbs>\date.vbs 2 30') do @SET file1=%%i

for /f "tokens=1-2 delims= " %%a in ("%date1%") do (
      set date=%%a%%b
)

for /f "tokens=1-2 delims= " %%a in ("%file1%") do (
      set file=%%a%%b
)


rem W3SVC1 Logs
cd /d "C:\inetpub\logs\LogFiles\W3SVC1"
"C:\Program Files\WinRAR\rar.exe" a %date%.rar -df -ri1 -ep1 -m5 -rr -r u_ex%file%.log

rem W3SVC2 Logs
cd /d "C:\inetpub\logs\LogFiles\W3SVC2"
"C:\Program Files\WinRAR\rar.exe" a %date%.rar -df -ri1 -ep1 -m5 -rr -r u_ex%file%.log
0
Comment
Question by:Mark James II
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
F P earned 500 total points
ID: 40937109
Doin. Too. Much.


http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage#02

sLogFolder = "C:\inetpub\logs\LogFiles\W3SVC1"
iMaxAge = 30   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateCreated
                if iFileAge > (iMaxAge+1)  then
                        objFSO.deletefile objFile, True
                end if
        Next
Next

Open in new window


sLogFolder = "C:\inetpub\logs\LogFiles\W3SVC2"
iMaxAge = 30   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateCreated
                if iFileAge > (iMaxAge+1)  then
                        objFSO.deletefile objFile, True
                end if
        Next
Next

Open in new window


All in one, or two separate vbs like I copied above, then follow the link I posted instructions.

Click the Actions tab, and then click New. In the New Action dialog box, select a value for Action, in this case, Start a program. In Program/script, enter cscript, and in Add arguments (optional), enter the path and name of the script file, for example, "C:\iis\Log_File_Deletion.vbs". Click OK.
0
 
LVL 8

Assisted Solution

by:F P
F P earned 500 total points
ID: 40937113
0
 
LVL 8

Assisted Solution

by:F P
F P earned 500 total points
ID: 40937118
Or powershell it:

http://serverfault.com/questions/65329/how-can-i-keep-iis-log-files-cleaned-up-regularly

set-location c:\windows\system32\Logfiles\W3SVC1\ -ErrorAction Stop
foreach ($File in get-childitem -include *.log) {
   if ($File.LastWriteTime -lt (Get-Date).AddDays(-30)) {
      del $File
   }
}

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question