<p>I'd like to write a script/batch that will bunch up my daily IIS logs and zip them up by month.</p>

<p>ex080801.log which is in the format of ex<em>yymmdd</em>.log</p>

<p>ex080801.log - ex080831.log gets zipped up and the log files deleted.</p>

<p>The reason we do this is because on a heavy site a log file for one day could be 500mb to 1gb so we zip them up which compresses them by 98% and dump the real log file.  We use webtrend to analyze the log files and it is capable of reading into a zip file.</p>

<p>Does anyone have any ideas on how to script this or would be willing to share some code?</p>

[{'answer_id': 29507, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 0, 'selected': False, 'text': "<p>Regex will do the trick... create a perl/python/php script to do the job for you..<br>\nI'm pretty sure windows batch file can't do regex.</p>\n"}, {'answer_id': 29609, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 5, 'selected': True, 'text': '<p>You\'ll need a command line tool to zip up the files. I recommend <a href="http://www.7-zip.org/download.html" rel="noreferrer">7-Zip</a> which is free and easy to use. The self-contained command line version (7za.exe) is the most portable choice.</p>\n\n<p>Here\'s a two-line batch file that would zip the log files and delete them afterwards:</p>\n\n<pre><code>7za.exe a -tzip ex%1-logs.zip %2\\ex%1*.log\ndel %2\\ex%1*.log\n</code></pre>\n\n<p>The first parameter is the 4 digit year-and-month, and the second parameter is the path to the directory containing your logs. For example: <code>ziplogs.bat 0808 c:\\logs</code></p>\n\n<p>It\'s possible to get more elaborate (i.e. searching the filenames to determine which months to archive). You might want to check out the Windows <a href="http://technet.microsoft.com/en-us/library/bb490907.aspx" rel="noreferrer">FINDSTR</a> command for searching input text with regular expressions.</p>\n'}, {'answer_id': 35030, 'author': 'Steve Moon', 'author_id': 3660, 'author_profile': 'https://Stackoverflow.com/users/3660', 'pm_score': 2, 'selected': False, 'text': '<p>We use a script like the following. Gzip is from the cygwin project. I\'m sure you could modify the syntax to use a zip tool instead. The "skip" argument is the number of files to not archive off -- we keep 11 days in the \'current\' directory.</p>\n\n<pre><code>@echo off\nsetlocal\nFor /f "skip=11 delims=/" %%a in (\'Dir D:\\logs\\W3SVC1\\*.log /B /O:-N /T:C\')do move "D:\\logs\\W3SVC1\\%%a" "D:\\logs\\W3SVC1\\old\\%%a"\nd:\ncd "\\logs\\W3SVC1\\old"\ngzip -n *.log\nEndlocal\nexit\n</code></pre>\n'}, {'answer_id': 280653, 'author': 'alimack', 'author_id': 39447, 'author_profile': 'https://Stackoverflow.com/users/39447', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s my script which basically adapts David\'s, and zips up last month\'s logs, moves them and deletes the original log files.  this can be adapted for Apache logs too.\nThe only problem with this is you may need to edit the replace commands, if your DOS date function outputs date of the week.\nYou\'ll also need to install 7-zip. </p>\n\n<p>You can also download IISlogslite but it compresses each day\'s file into a single zip file which I didn\'t find useful.  There is a vbscript floating about the web that does the same thing.</p>\n\n<pre>\n-------------------------------------------------------------------------------------\n@echo on\n\n:: Name - iislogzip.bat\n:: Description - Server Log File Manager\n::\n:: History\n:: Date         Authory      Change\n:: 27-Aug-2008  David Crow   Original (found on stack overflow)\n:: 15-Oct-2008  AIMackenzie  Slimmed down commands\n\n\n:: ========================================================\n:: setup variables and parameters\n:: ========================================================\n:: generate date and time variables\n\nset month=%DATE:~3,2%\nset year=%DATE:~8,2%\n\n::Get last month and check edge conditions\n\nset /a lastmonth=%month%-1\nif %lastmonth% equ 0 set /a year=%year%-1\nif %lastmonth% equ 0 set lastmonth=12\nif %lastmonth% lss 10 set lastmonth=0%lastmonth%\n\nset yymm=%year%%lastmonth%\n\nset logpath="C:\\WINDOWS\\system32\\LogFiles"\nset zippath="C:\\Program Files\\7-Zip\\7z.exe"\nset arcpath="C:\\WINDOWS\\system32\\LogFiles\\WUDF"\n\n\n:: ========================================================\n:: Change to log file path\n:: ========================================================\ncd /D %logpath%\n\n:: ========================================================\n:: zip last months IIS log files, move zipped file to archive \n:: then delete old logs\n:: ========================================================\n%zippath% a -tzip ex%yymm%-logs.zip %logpath%\\ex%yymm%*.log\nmove "%logpath%\\*.zip" "%arcpath%"\ndel %logpath%\\ex%yymm%*.log\n</pre>\n'}, {'answer_id': 410448, 'author': 'Cheeso', 'author_id': 48082, 'author_profile': 'https://Stackoverflow.com/users/48082', 'pm_score': 1, 'selected': False, 'text': '<p>You can grab the command-line utilities package from <a href="http://www.codeplex.com/DotNetZip" rel="nofollow noreferrer">DotNetZip</a> to get tools to create zips from scripts.  There\'s a nice little tool called Zipit.exe that runs on the command line, adds files or directories to zip files.   It is fast, efficient.  </p>\n\n<p>A better option might be to just do the zipping from within PowerShell. </p>\n\n<pre><code>function ZipUp-Files ( $directory )\n{\n\n  $children = get-childitem -path $directory\n  foreach ($o in $children) \n  {\n    if ($o.Name -ne "TestResults" -and \n        $o.Name -ne "obj" -and \n        $o.Name -ne "bin" -and \n        $o.Name -ne "tfs" -and \n        $o.Name -ne "notused" -and \n        $o.Name -ne "Release")\n    {\n      if ($o.PSIsContainer)\n      {\n        ZipUp-Files ( $o.FullName )\n      }\n      else \n      {\n        if ($o.Name -ne ".tfs-ignore" -and\n           !$o.Name.EndsWith(".cache") -and\n           !$o.Name.EndsWith(".zip") )\n        {\n          Write-output $o.FullName\n          $e= $zipfile.AddFile($o.FullName)\n        }\n      }\n    }\n  }\n}\n\n\n[System.Reflection.Assembly]::LoadFrom("c:\\\\\\bin\\\\Ionic.Zip.dll");\n\n$zipfile =  new-object Ionic.Zip.ZipFile("zipsrc.zip");\n\nZipUp-Files "DotNetZip"\n\n$zipfile.Save()\n</code></pre>\n'}, {'answer_id': 2366322, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Borrowed zip function from <a href="http://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx</a></p>\n\n<p>Here is powershell answer that works wonders:</p>\n\n<pre><code>param([string]$Path = $(read-host "Enter the path"))\nfunction New-Zip\n{\n    param([string]$zipfilename)\n    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))\n    (dir $zipfilename).IsReadOnly = $false\n}\nfunction Add-Zip\n{\n    param([string]$zipfilename)\n\n    if(-not (test-path($zipfilename)))\n    {\n        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))\n        (dir $zipfilename).IsReadOnly = $false  \n    }\n\n    $shellApplication = new-object -com shell.application\n    $zipPackage = $shellApplication.NameSpace($zipfilename)\n\n    foreach($file in $input) \n    { \n            $zipPackage.CopyHere($file.FullName)\n            Start-sleep -milliseconds 500\n    }\n}\n$FilesToZip = dir $Path -recurse -include *.log\nforeach ($file in $FilesToZip) {\nNew-Zip $file.BaseName\ndir $($file.directoryname+"\\"+$file.name) | Add-zip $($file.directoryname+"\\$($file.basename).zip")\ndel $($file.directoryname+"\\"+$file.name)\n}\n</code></pre>\n'}, {'answer_id': 21876124, 'author': 'leb', 'author_id': 3327337, 'author_profile': 'https://Stackoverflow.com/users/3327337', 'pm_score': 1, 'selected': False, 'text': '<p>We use this powershell script: <a href="http://gallery.technet.microsoft.com/scriptcenter/31db73b4-746c-4d33-a0aa-7a79006317e6" rel="nofollow">http://gallery.technet.microsoft.com/scriptcenter/31db73b4-746c-4d33-a0aa-7a79006317e6</a></p>\n\n<p>It uses 7-zip and verifys the files before deleting them</p>\n'}]
['https://Stackoverflow.com/questions/29496', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/648/']