<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ShinyLight Development &#187; IIS</title>
	<atom:link href="http://www.shinylight.com/category/systems/iis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shinylight.com</link>
	<description>Web Development and Other Goodness in the World of a Software Engineer.</description>
	<lastBuildDate>Mon, 07 Jun 2010 03:02:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>IIS Logs Scripts</title>
		<link>http://www.shinylight.com/2009/12/12/iis-logs-scripts/</link>
		<comments>http://www.shinylight.com/2009/12/12/iis-logs-scripts/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 06:19:35 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[IIS]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.shinylight.com/?p=560</guid>
		<description><![CDATA[While working with some IIS logs, I decided to start practicing my Python. I put together some handy Python functions to work with IIS Log files. These will come in handy. On a 3GB, 2.5GHz, running WinXP machine, these functions take about 3 seconds to process a 180MB Text file. Python code could be optimized [...]]]></description>
			<content:encoded><![CDATA[<p>While working with some IIS logs, I decided to start practicing my Python. I put together some handy Python functions to work with IIS Log files. These will come in handy. On a 3GB, 2.5GHz, running WinXP machine, these functions take about 3 seconds to process a 180MB Text file. Python code could be optimized to be faster if you&#8217;re dealing with larger sized files.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># An IIS log file can have various log properties. Everytime you add new columns to log for</span>
<span style="color: #808080; font-style: italic;"># in IIS, it creates a new row full of columns.</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
MainLogDelimiter = <span style="color: #483d8b;">&quot;#Software: Microsoft Internet Information Services 6.0&quot;</span>
TestFile         = <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Dan<span style="color: #000099; font-weight: bold;">\\</span>IIS-Log-Import<span style="color: #000099; font-weight: bold;">\\</span>Logs<span style="color: #000099; font-weight: bold;">\\</span>not-the-same.txt&quot;</span>
BigTestFile      = <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Dan<span style="color: #000099; font-weight: bold;">\\</span>IIS-Log-Import<span style="color: #000099; font-weight: bold;">\\</span>Logs<span style="color: #000099; font-weight: bold;">\\</span>ex090914<span style="color: #000099; font-weight: bold;">\\</span>ex090914.log&quot;</span>
LogsDir          = <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>Dan<span style="color: #000099; font-weight: bold;">\\</span>IIS-Log-Import<span style="color: #000099; font-weight: bold;">\\</span>Logs&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> SearchForFile<span style="color: black;">&#40;</span> rootpath, searchfor, includepath = <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>:
&nbsp;
  <span style="color: #808080; font-style: italic;"># Search for a file recursively from a root directory.</span>
  <span style="color: #808080; font-style: italic;">#  rootpath  = root directory to start searching from.</span>
  <span style="color: #808080; font-style: italic;">#  searchfor = regexp to search for, e.g.:</span>
  <span style="color: #808080; font-style: italic;">#                 search for *.jpg : \.exe$                     </span>
  <span style="color: #808080; font-style: italic;">#  includepath = appends the full path to the file</span>
  <span style="color: #808080; font-style: italic;">#                this attribute is optional</span>
  <span style="color: #808080; font-style: italic;"># Returns a list of filenames that can be used to loop</span>
  <span style="color: #808080; font-style: italic;"># through.</span>
  <span style="color: #808080; font-style: italic;">#</span>
  <span style="color: #808080; font-style: italic;"># TODO: Use the glob module instead. Could be faster.  </span>
  names = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  append = <span style="color: #483d8b;">&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span> rootpath <span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> files:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span> searchfor, name <span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> includepath == <span style="color: #ff4500;">0</span>:
          root = <span style="color: #483d8b;">&quot;&quot;</span>          
        <span style="color: #ff7700;font-weight:bold;">else</span>:          
          append = <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>
        names.<span style="color: black;">append</span><span style="color: black;">&#40;</span> root + append + name <span style="color: black;">&#41;</span>        
  <span style="color: #ff7700;font-weight:bold;">return</span> names  
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> isSameLogProperties<span style="color: black;">&#40;</span> FILE <span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># Tests to see if a log file has the same number of columns throughout</span>
  <span style="color: #808080; font-style: italic;"># This is in case new column properties were added/subtracted in the course</span>
  <span style="color: #808080; font-style: italic;"># of the log file.</span>
  FILE.<span style="color: black;">seek</span><span style="color: black;">&#40;</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>
  SubLogs = FILE.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span> MainLogDelimiter <span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># SubLogs[0] Stores the number of different log variations in the log file  </span>
  SubLogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span> SubLogs <span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span>    
&nbsp;
  <span style="color: #808080; font-style: italic;"># Grab the column names from the log file, separated by space</span>
  columns = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;^#Fields:<span style="color: #000099; font-weight: bold;">\s</span>([<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\-</span>()<span style="color: #000099; font-weight: bold;">\s</span>]+)$&quot;</span>, SubLogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span> | <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span> <span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>   
  LogSameProperties = <span style="color: #008000;">True</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span> <span style="color: #ff4500;">2</span>, SubLogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span> <span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># If there are columns</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span> columns <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>:    
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span> columns <span style="color: #66cc66;">!</span>= <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;^#Fields:<span style="color: #000099; font-weight: bold;">\s</span>([<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\-</span>()<span style="color: #000099; font-weight: bold;">\s</span>]+)$&quot;</span>, SubLogs<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span> | <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span> <span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:        
        LogSameProperties = <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">break</span>  
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> LogSameProperties
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getFirstColumn<span style="color: black;">&#40;</span> FILE <span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># This gets the columns from a log file. It returns only the first columns, and ignores another column</span>
  <span style="color: #808080; font-style: italic;"># row that may exist in case new columns were added/subtracted in IIS. </span>
  <span style="color: #808080; font-style: italic;"># input: FILE</span>
  <span style="color: #808080; font-style: italic;"># output: 1 single element List</span>
  FILE.<span style="color: black;">seek</span><span style="color: black;">&#40;</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>
  names = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  <span style="color: #808080; font-style: italic;"># Grab the column names from the log file, separated by space</span>
  names.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;^#Fields:<span style="color: #000099; font-weight: bold;">\s</span>([<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\-</span>()<span style="color: #000099; font-weight: bold;">\s</span>]+)$&quot;</span>, FILE.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span> MainLogDelimiter <span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span> | <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span> <span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> names
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getAllColumns<span style="color: black;">&#40;</span> FILE <span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># This gets all the columns from a log file. </span>
  <span style="color: #808080; font-style: italic;"># input: FILE</span>
  <span style="color: #808080; font-style: italic;"># output: List</span>
  FILE.<span style="color: black;">seek</span><span style="color: black;">&#40;</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>  
  names = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  SubLogs = FILE.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span> MainLogDelimiter <span style="color: black;">&#41;</span>    
  <span style="color: #808080; font-style: italic;"># SubLogs[0] Stores the number of different log variations in the log file  </span>
  SubLogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span> SubLogs <span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span>        
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span> <span style="color: #ff4500;">1</span>, SubLogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span> <span style="color: black;">&#41;</span>:        
    names.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;^#Fields:<span style="color: #000099; font-weight: bold;">\s</span>([<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\-</span>()<span style="color: #000099; font-weight: bold;">\s</span>]+)$&quot;</span>, SubLogs<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span> | <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span> <span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>  
  <span style="color: #ff7700;font-weight:bold;">return</span> names  
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># EXAMPLE:</span>
<span style="color: #808080; font-style: italic;"># Loop through all the IIS log files in the directory</span>
<span style="color: #808080; font-style: italic;"># for file in SearchForFile( LogsDir, &quot;\.txt$&quot;, 1 ):  </span>
LogFile = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span> <span style="color: #008000;">file</span>, <span style="color: #483d8b;">&quot;r&quot;</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span> isSameLogProperties<span style="color: black;">&#40;</span> LogFile <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">file</span>, <span style="color: #483d8b;">&quot;the same&quot;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">file</span>, <span style="color: #483d8b;">&quot;not the same&quot;</span>
LogFile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em></em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;submitHeadline=IIS+Logs+Scripts&amp;submitSummary=" rel="nofollow" title="Add to&nbsp;Buzz"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/buzz.png" title="Add to&nbsp;Buzz" alt="Add to&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;title=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;title=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://www.shinylight.com/2009/12/12/iis-logs-scripts/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;title=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;bm_description=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;T=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;title=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;title=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.shinylight.com/2009/12/12/iis-logs-scripts/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http://www.shinylight.com/2009/12/12/iis-logs-scripts/" rel="nofollow" title="Add to&nbsp;Tip'd"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/tipd.png" title="Add to&nbsp;Tip'd" alt="Add to&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+IIS+Logs+Scripts+@+http://www.shinylight.com/2009/12/12/iis-logs-scripts/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://www.shinylight.com/2009/12/12/iis-logs-scripts/&amp;t=IIS+Logs+Scripts" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.shinylight.com/2009/12/12/iis-logs-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove Comments from IIS Logs</title>
		<link>http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/</link>
		<comments>http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 06:22:32 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[IIS]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Systems]]></category>
		<category><![CDATA[iis logs perl server]]></category>

		<guid isPermaLink="false">http://www.shinylight.com/?p=524</guid>
		<description><![CDATA[If you think that Log Parser is a bit on the slow side (i.e. if you&#8217;re dealing with big IIS logs) and you want to bulk import your logs into SQL Server, then you&#8217;ll have to remove # comments from the log files. Microsoft has the PrepWebLog Utility to do this, but it seems to [...]]]></description>
			<content:encoded><![CDATA[<p>If you think that <a href="http://www.shinylight.com/2009/09/23/logparser-to-query-iis-logs-using-sql/">Log Parser</a> is a bit on the slow side (i.e. if you&#8217;re dealing with big IIS logs) and you want to bulk import your logs into SQL Server, then you&#8217;ll have to remove # comments from the log files. Microsoft has the <a href="http://support.microsoft.com/kb/296093/EN-US/">PrepWebLog Utility</a> to do this, but it seems to choke for files that are > 100 MB. Also, you&#8217;ll have to write this as a batch file so it goes through a whole directory of files. </p>
<p>I wrote a Perl script that&#8217;s relatively fast (faster than PrepWebLog) and it can crawl folders/subfolders recursively. Here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># parse.pl</span>
<span style="color: #666666; font-style: italic;"># example: </span>
<span style="color: #666666; font-style: italic;">#   parse c:\temp\logs\logs*\*.log</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Requirement: no spaces in the directory names and file names.</span>
<span style="color: #666666; font-style: italic;"># This gets called via run.bat. </span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getFileList 
<span style="color: #009900;">&#123;</span>    
    <span style="color: #666666; font-style: italic;"># This function returns an array of file list based on filter</span>
    <span style="color: #666666; font-style: italic;"># This is the filter they can put in.       </span>
    <span style="color: #666666; font-style: italic;"># Returns a file with full path. </span>
    <span style="color: #666666; font-style: italic;"># Example of filters: getFileList ( &quot;*.log&quot; );</span>
    <span style="color: #0000ff;">@files</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;</span><span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">@files</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> remove_comments
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Remove # pound sign comments from files. </span>
  <span style="color: #666666; font-style: italic;"># @_[0] = filename</span>
&nbsp;
  <span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$in</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> 
      <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;in: @_[0]&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$out</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;@_[0].txt&quot;</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;out: @_[0]&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$line</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$in</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$out</span> <span style="color: #0000ff;">$line</span>
          <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$line</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^#/</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066;">close</span> <span style="color: #0000ff;">$in</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">close</span> <span style="color: #0000ff;">$out</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">########## MAIN #############</span>
<span style="color: #0000ff;">$arg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Location of root directory of logs files</span>
<span style="color: #666666; font-style: italic;">#$arg = 'c:\temp\logs\logs*\*.log';</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Replace slashes</span>
<span style="color: #0000ff;">$arg</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/\\/\\\\/g</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Loop through all the log files. </span>
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$file</span> <span style="color: #009900;">&#40;</span>getFileList <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>  
  <span style="color: #000066;">print</span> <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Processing file $file ... <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
  remove_comments<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$file</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Perl script gets called via run.bat:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">REM No spaces in directory and file names.</span>
perl Parse.pl D:\statesites\W3SVC*\*.log
<span style="color: #b1b100; font-weight: bold;">pause</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em></em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;submitHeadline=Remove+Comments+from+IIS+Logs&amp;submitSummary=" rel="nofollow" title="Add to&nbsp;Buzz"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/buzz.png" title="Add to&nbsp;Buzz" alt="Add to&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;title=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;title=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;title=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;bm_description=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;T=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;title=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;title=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/" rel="nofollow" title="Add to&nbsp;Tip'd"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/tipd.png" title="Add to&nbsp;Tip'd" alt="Add to&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Remove+Comments+from+IIS+Logs+@+http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/&amp;t=Remove+Comments+from+IIS+Logs" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://www.shinylight.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.shinylight.com/2009/12/11/remove-comments-from-iis-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
