Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-17
4
Medium Priority
?
115 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 2000 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 2000 total points
ID: 40937113
0
 
LVL 8

Assisted Solution

by:F P
F P earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

926 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