Solved

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

Posted on 2015-02-17
4
52 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.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now