Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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