Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

18 Experts available now in Live!

Get 1:1 Help Now