<?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>youdidwhatwithtsql.com &#187; Data</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/data/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Get TFL Tube data with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689</link>
		<comments>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689#comments</comments>
		<pubDate>Sat, 27 Feb 2010 19:18:24 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[data.gov.uk]]></category>
		<category><![CDATA[London Datastore]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689</guid>
		<description><![CDATA[The London Datastore has loads of datasets available that we can use for free. One of the datasets available is a list of TFL Station Locations. The station location feed is a geo-coded KML feed of most of London Underground, DLR and London Overground stations. Here&#8217;s Powershell script that will extract this data from a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689">Get TFL Tube data with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://data.london.gov.uk/" target="_blank">London Datastore</a> has loads of datasets available that we can use for free. One of the datasets available is a list of <a href="http://data.london.gov.uk/datastore/package/tfl-station-locations" target="_blank">TFL Station Locations</a>. The station location feed is a geo-coded KML feed of most of London Underground, DLR and London Overground stations. Here&#8217;s <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that will extract this data from a url and write it to a pipe-delimited file ready for import into the database of your choice. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p689code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6892"><td class="code" id="p689code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> XML
<span style="color: #800080;">$url</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;http://www.tfl.gov.uk/tfl/syndication/feeds/stations.kml&quot;</span>
<span style="color: #800080;">$csvFile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$env:UserProfile\Desktop\tfl_tubes.csv&quot;</span>;
&nbsp;
<span style="color: #008000;"># Empty file if it already exists</span>
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800080;">$null</span>;
<span style="color: #008000;"># Add headers to file</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800000;">&quot;Station|Address|Coordinates&quot;</span>;
&nbsp;
<span style="color: #008000;"># Load the xml</span>
<span style="color: #800080;">$xml</span>.Load<span style="color: #000000;">&#40;</span><span style="color: #800080;">$url</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$stations</span> <span style="color: pink;">=</span> <span style="color: #800080;">$xml</span>.kml.Document.Placemark;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$station</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$stations</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.name;
	<span style="color: #800080;">$description</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.description;
	<span style="color: #800080;">$coordinates</span> <span style="color: pink;">=</span> <span style="color: #800080;">$station</span>.Point.coordinates
	<span style="color: #008000;"># This data needs cleaning a bit</span>
	<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$name</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$description</span> <span style="color: pink;">=</span> <span style="color: #800080;">$description</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$coordinates</span> <span style="color: pink;">=</span> <span style="color: #800080;">$coordinates</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008000;"># Ad line to the csv file</span>
	<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csvFile</span> <span style="color: #800000;">&quot;$name|$description|$coordinates&quot;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>After running the script check your desktop for a file called <strong>tfl_tubes.csv</strong> which should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/tfl_tubes.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="TFL Station Locations csv data file" border="0" alt="tfl tubes thumb Get TFL Tube data with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/tfl_tubes_thumb.png" width="244" height="183" /></a></p>
<p><map name='google_ad_map_689_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/689?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_689_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=689&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fget-tfl-tube-data-with-powershell%2F689' title="Get TFL Tube data with Powershell" alt=" Get TFL Tube data with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689">Get TFL Tube data with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/get-tfl-tube-data-with-powershell/689/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>British Government launches free data site</title>
		<link>http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560</link>
		<comments>http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560#comments</comments>
		<pubDate>Wed, 20 Jan 2010 21:46:14 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[data.gov.uk]]></category>
		<category><![CDATA[free datasets]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560</guid>
		<description><![CDATA[Just a quick post about the British Governments launch of data.gov.uk offering free access to a large number of datasets. It&#8217;s good to see this data, that we as taxpayers have funded, available for free use. Hats off to The Guardian&#8217;s long running free our data campaign. The site is being officially launched by Sir [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560">British Government launches free data site</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Just a quick post about the British Governments launch of <a href="http://data.gov.uk" target="_blank">data.gov.uk</a> offering free access to a large number of datasets. It&#8217;s good to see this data, that we as taxpayers have funded, available for free use. Hats off to <a href="http://www.guardian.co.uk" target="_blank">The Guardian&#8217;s</a> long running <a href="http://www.guardian.co.uk/technology/free-our-data" target="_blank">free our data</a> campaign.</p>
<p>The site is being officially launched by <a href="http://en.wikipedia.org/wiki/Tim_Berners-Lee" target="_blank">Sir Tim Berners Lee</a> tomorrow, but you can access <a href="http://data.gov.uk" target="_blank">the site</a> now. The US has their own version <a href="http://data.gov" target="_blank">data.gov</a> but the British site boasts nearly three times as many datasets. Take a peek at the <a href="http://data.gov.uk/data/all" target="_blank">available datasets</a>. It always fantastic looking through the great number of <a href="http://data.gov.uk/apps" target="_blank">apps</a> that people create around freely available data. Why not take a look and start your own project?</p>
<p>Those interested in London specific data can check out the <a href="http://data.london.gov.uk/" target="_blank">London DataStore.</a> People in sunny San Francisco have <a href="http://datasf.org/" target="_blank">datasf</a> and Chicago also have a <a href="http://egov.cityofchicago.org/CityData/CityData.html" target="_blank">dataset section</a> on their website. Read <a href="http://www.guardian.co.uk/technology/2010/jan/20/tim-berners-lee-free-data" target="_blank">more about this</a>.</p>
<p><map name='google_ad_map_560_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/560?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_560_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=560&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fbritish-government-launches-free-data-site%2F560' title="British Government launches free data site" alt=" British Government launches free data site" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560">British Government launches free data site</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/british-government-launches-free-data-site/560/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting CSV FileS to XML with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</link>
		<comments>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408#comments</comments>
		<pubDate>Wed, 28 Oct 2009 21:34:58 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</guid>
		<description><![CDATA[Powershell is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml. ?View Code POWERSHELL# csv file to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code6'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4086"><td class="code" id="p408code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># csv file to convert</span>
<span style="color: #800080;">$csv</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.csv&quot;</span>;
<span style="color: #008000;"># xml file to create</span>
<span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.xml&quot;</span>;
&nbsp;
<span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csv</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$xml</span>;</pre></td></tr></table></div>

<p>This will produce xml looking something like this.</p>
<p>&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;objs xmlns=&quot;http://schemas.microsoft.com/powershell/2004/04&quot; version=&quot;1.1.0.1&quot;&gt;<br />
  &lt;obj refid=&quot;0&quot;&gt;<br />
    &lt;tn refid=&quot;0&quot;&gt;<br />
      &lt;t&gt;System.Management.Automation.PSCustomObject&lt;/t&gt;<br />
      &lt;t&gt;System.Object&lt;/t&gt;<br />
    &lt;/tn&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Rhys&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Campbell&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;29&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;1&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Joe&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Bloggs&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;40&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;2&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Steve&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Smith&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;35&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
&lt;/objs&gt;</p>
<p>With the inclusion of the <a href="http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-childitem.mspx" target="_blank">Get-ChildItem cmdlet</a> we can merge multiple csv files into a single xml file with just a few lines of code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code7'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4087"><td class="code" id="p408code7"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Folder of csv files</span>
<span style="color: #800080;">$csvFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\*&quot;</span> <span style="color: #008080; font-style: italic;">-Include</span> <span style="color: pink;">*</span>.csv;
&nbsp;
<span style="color: #008000;"># Process each csv file. Need to be the same structure</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$csvFiles</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$csvContent</span> <span style="color: pink;">+=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$file</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Export the imported data as one xml file</span>
<span style="color: #800080;">$csvContent</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml;</pre></td></tr></table></div>

<p>Turning this back into a csv file is a simple <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> one-liner!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4088"><td class="code" id="p408code8"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Turn this back into csv</span>
<span style="color: #008080; font-weight: bold;">Import-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\BackToCsv.csv <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;</pre></td></tr></table></div>

<p>Check out my other data related Powershell posts; <a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374" target="_blank">splitting csv files with Powershell</a>, <a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" target="_blank">merging csv files with Powershell</a> and <a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388" target="_blank">trimming whitespace with Powershell</a>.</p>
<p><map name='google_ad_map_408_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/408?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_408_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=408&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fconverting-csv-files-to-xml-with-powershell%2F408' title="Converting CSV FileS to XML with Powershell" alt=" Converting CSV FileS to XML with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Splitting csv files with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374</link>
		<comments>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374#comments</comments>
		<pubDate>Wed, 23 Sep 2009 21:59:21 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374</guid>
		<description><![CDATA[I’ve blogged before about the usefulness of Powershell for data tasks. A few weeks ago I had a requirement at work for merging csv files and recently I needed to split a single csv file into several files. While this is easy to do using SSIS and a bit of T-SQL it is a little [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374">Splitting csv files with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’ve blogged before about the usefulness of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> for data tasks. A few weeks ago I had a requirement at work for <a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" target="_blank">merging csv files</a> and recently I needed to split a single csv file into several files.</p>
<p>While this is easy to do using <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> and a bit of <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> it is a little tedious, and more time consuming, that it needs to be for adhoc jobs. Again Powershell came to the rescue! Here’s an example.</p>
<p>First I created a csv called bigCsvfile.csv, with 1, 000 records of data and placed it into my user profile directory; <strong>C:\Users\Rhys</strong> on my laptop. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/splitting_csv_files_with_powershell_1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="splitting_csv_files_with_powershell_1" border="0" alt="splitting csv files with powershell 1 thumb Splitting csv files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/splitting_csv_files_with_powershell_1_thumb.png" width="169" height="244" /></a> </p>
</p>
<p>This <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script does require a sequential integer id for each record to work correctly as it uses this to divide up the records. Now assuming you have a csv file, called <strong>bigCsvFile.csv</strong>,<strong> </strong>in the correct location then the only thing you should need to change is the <strong>$split </strong>variable value. This is now many files you would like to split the original file into.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p374code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37410"><td class="code" id="p374code10"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Author: Rhys Campbell</span>
<span style="color: #008000;"># 2009-09-22</span>
<span style="color: #008000;"># Splitting up csv files</span>
&nbsp;
<span style="color: #008000;"># Path to csv file. Must contain a sequential unique integer id column </span>
<span style="color: #800080;">$csvFile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\bigcsvFile.csv&quot;</span>;
<span style="color: #008000;"># Slit into how many files?</span>
<span style="color: #800080;">$split</span> <span style="color: pink;">=</span> <span style="color: #804000;">10</span>;
&nbsp;
<span style="color: #008000;"># Get the csv file content</span>
<span style="color: #800080;">$content</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$csvFile</span>;
&nbsp;
<span style="color: #008000;"># So we start from Id = 1 in the csv file</span>
<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
<span style="color: #800080;">$end</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># calc records per file</span>
<span style="color: #800080;">$records_per_file</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #800080;">$content</span>.Count <span style="color: pink;">/</span> <span style="color: #800080;">$split</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$split</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Set the end value for selecting records</span>
	<span style="color: #800080;">$end</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$records_per_file</span>;
	<span style="color: #008000;"># Need to cast to int or we get an alphabetic comparison when we want a numeric one</span>
	<span style="color: #800080;">$content</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000080;">$_</span>.Id <span style="color: #FF0000;">-ge</span> <span style="color: #800080;">$start</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000080;">$_</span>.Id <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$end</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\file$i.csv&quot;</span> <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;
	<span style="color: #008000;"># Update start value for selecting records</span>
	<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #800080;">$end</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Once the script has executed successfully it will create the split csv files in your user profile folder. The new files are named sequentially, i.e. file1.csv, file2.csv, file3.csv etc.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/split_csv_files.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="split_csv_files" border="0" alt="split csv files thumb Splitting csv files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/split_csv_files_thumb.png" width="244" height="150" /></a> </p>
<p>Depending on the number of records, and the number of files split to, the records may not be the same in each file. Provided the number of records versus file split is reasonable it will be pretty close. Now with a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script it takes just a few moments to perform those previously tedious tasks!</p>
<p><map name='google_ad_map_374_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/374?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_374_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=374&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsplitting-csv-files-with-powershell%2F374' title="Splitting csv files with Powershell" alt=" Splitting csv files with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374">Splitting csv files with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Merging CSV Files with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330</link>
		<comments>http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330#comments</comments>
		<pubDate>Mon, 24 Aug 2009 11:24:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330</guid>
		<description><![CDATA[Powershell is really useful for documenting and managing your servers but it’s also a pretty good tool for working with data. I’ve been using it to merge csv files, with an identical structure, into a single file. Now this is pretty easy, if rather tedious, to do using SQL Server Import / Export functionality or [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330">Merging CSV Files with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> is really useful for documenting and managing your servers but it’s also a pretty good tool for working with data. I’ve been using it to merge csv files, with an identical structure, into a single file. Now this is pretty easy, if rather tedious, to do using <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> Import / Export functionality or with SSIS. <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> makes this a snap!</p>
<p>In this example I have two csv files in a directory</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv files" border="0" alt="image thumb Merging CSV Files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb.png" width="244" height="205" /></a> </p>
<p>These just contain some simple name and age data.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Content of my test csv files" border="0" alt="image thumb1 Merging CSV Files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb1.png" width="241" height="244" /></a> </p>
<p>This simple script will produce a third file, merging the contents of file1.csv and file2.csv.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p330code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33012"><td class="code" id="p330code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Author: Rhys Campbell</span>
<span style="color: #008000;"># 2009-08-22</span>
<span style="color: #008000;"># Merging identical csv files</span>
&nbsp;
<span style="color: #008000;"># Directory containing csv files, include *.*</span>
<span style="color: #800080;">$directory</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\*.*&quot;</span>;
<span style="color: #008000;"># Get the csv files</span>
<span style="color: #800080;">$csvFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$directory</span> <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: pink;">*</span>.csv;
&nbsp;
<span style="color: #008000;"># Updated 01/03/2010. Thanks to comment from Chris.</span>
<span style="color: #008000;"># Resolves error Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.</span>
<span style="color: #008000;">#$content = $null;</span>
<span style="color: #800080;">$content</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Process each file</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$csv</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$csvFiles</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$content</span> <span style="color: pink;">+=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$csv</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Write a datetime stamped csv file</span>
<span style="color: #800080;">$datetime</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;yyyyMMddhhmmss&quot;</span>;
<span style="color: #800080;">$content</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\merged_$datetime.csv&quot;</span> <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;</pre></td></tr></table></div>

<p>If all goes as planned a new file will be created.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv folder with new file" border="0" alt="image thumb2 Merging CSV Files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb2.png" width="244" height="180" /></a> </p>
<p>This file will contain data from both csv files.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The final merged csv file" border="0" alt="image thumb3 Merging CSV Files with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb3.png" width="244" height="182" /></a> </p>
<p>This is obviously a fairly trivial example but it’s a massive timesaver when you have many such files to merge. Word of warning, if you’ve got very big files, you may want to change the script to use <a href="http://technet.microsoft.com/en-us/library/dd347594.aspx" target="_blank">Add-Content</a>, to flush each csv file to disk in the foreach loop, to avoid munching up all your RAM.</p>
<p><map name='google_ad_map_330_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/330?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_330_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=330&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmerging-csv-files-with-powershell%2F330' title="Merging CSV Files with Powershell" alt=" Merging CSV Files with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330">Merging CSV Files with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

