<?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; csv</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/csv/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>TSQL: Query Pipe-Delimited text files with OPENROWSET</title>
		<link>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429</link>
		<comments>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429#comments</comments>
		<pubDate>Tue, 10 Nov 2009 21:49:06 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[OPENROWSET]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[text files]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[vertical bar]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429</guid>
		<description><![CDATA[Sometimes, when working with extracts of data, it can be a pain to have to load these files into a database in order to work with them. It&#8217;s easy to use OPENROWSET to save yourself a little time. Here&#8217;s a basic example; ?View Code TSQLSELECT * FROM OPENROWSET &#40;'MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\Users\Rhys\Desktop\csv;Extended [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429">TSQL: Query Pipe-Delimited text files with OPENROWSET</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when working with extracts of data, it can be a pain to have to load these files into a database in order to work with them. It&#8217;s easy to use <a href="http://technet.microsoft.com/en-us/library/ms190312.aspx" target="_blank">OPENROWSET</a> to save yourself a little time. Here&#8217;s a basic example;</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('p429code3'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4293"><td class="code" id="p429code3"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">OPENROWSET</span>
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'MSDASQL'</span>,
<span style="color: #FF0000;">'Driver={Microsoft Text Driver (*.txt; *.csv)}; 
DefaultDir=C:<span style="color: #000099; font-weight: bold;">\U</span>sers<span style="color: #000099; font-weight: bold;">\R</span>hys<span style="color: #000099; font-weight: bold;">\D</span>esktop<span style="color: #000099; font-weight: bold;">\c</span>sv;Extended properties='</span><span style="color: #FF0000;">'ColNameHeader=True;Format=Delimited;'</span><span style="color: #FF0000;">''</span>,
<span style="color: #FF0000;">'SELECT * FROM file1.csv'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>The <strong>DefaultDir</strong> property points to a directory containing standard comma separated files. Now the text files can be treated just like tables, note the SELECT query above in red text. There can be any number of files in the same directory and they can be queried simply by changing the appropriate filename, i.e. <strong>file1.csv</strong> to <strong>file2.txt</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_openrowset_sql_server.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv openrowset sql server" border="0" alt="csv openrowset sql server thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_openrowset_sql_server_thumb.png" width="206" height="162" /></a> </p>
<p>If you experience the below error you need to enable the feature on your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instance.</p>
<pre>Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see &quot;Surface Area Configuration&quot; in SQL Server Books Online. </pre>
<p>Run the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to enable.</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('p429code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4294"><td class="code" id="p429code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'Ad Hoc Distributed Queries'</span>, <span style="color: #000;">1</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;</pre></td></tr></table></div>

</p>
<p>Now this is handy but some people, myself included, prefer to use Pipe-Delimited csv files. <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> seem insistent on calling pipes the <a href="http://en.wikipedia.org/wiki/Vertical_bar" target="_blank">vertical-bar</a>. If you attempt the above, with pipe-delimited files, here&#8217;s what you get!</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv pipe openrowset sql server" border="0" alt="csv pipe openrowset sql server thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_thumb.png" width="188" height="163" /></a> </p>
<p>Not something we can work with very easily! By modifying a registry entry we can quickly resolve this. Usual warnings about editing your registry apply, i.e. <a href="http://support.microsoft.com/kb/322756" target="_blank">backup</a> and know what you&#8217;re doing.</p>
<p>Using <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tools_regeditors.mspx?mfr=true" target="_blank">regedit</a>, navigate to the following key;</p>
<pre>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text</pre>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/regedit_jet_text_driver.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="regedit jet text driver" border="0" alt="regedit jet text driver thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/regedit_jet_text_driver_thumb.png" width="244" height="159" /></a> </p>
<p>Edit the value for the <strong>Format </strong>key;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jet text driver format registry" border="0" alt="text driver format registry thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_thumb.png" width="244" height="111" /></a> </p>
<p>Change the value from <strong>CSVDelimited </strong>to <strong>Delimited(|)</strong>. To reverse this change just set the value back to <strong>CSVDelimited</strong> when you need to.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_pipe.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jet text driver format registry pipe" border="0" alt="text driver format registry pipe thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_pipe_thumb.png" width="244" height="111" /></a> </p>
<p>Run the same query again and you&#8217;ll see our data is how we need it to be!</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_fixed.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv pipe openrowset sql server fixed" border="0" alt="csv pipe openrowset sql server fixed thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_fixed_thumb.png" width="209" height="165" /></a> </p>
</p>
<p>Read more about the <a href="http://msdn.microsoft.com/en-us/library/bb177651.aspx" target="_blank">Text Data Source Driver</a> if you need to query files with other types of delimiters.</p>
<p><map name='google_ad_map_429_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/429?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_429_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=429&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftsql-query-pipe-delimited-text-files-with-openrowset%2F429' title="TSQL: Query Pipe Delimited text files with OPENROWSET" alt=" TSQL: Query Pipe Delimited text files with OPENROWSET" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429">TSQL: Query Pipe-Delimited text files with OPENROWSET</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429/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('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;"># 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('p408code9'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4089"><td class="code" id="p408code9"><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('p408code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40810"><td class="code" id="p408code10"><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('p374code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37412"><td class="code" id="p374code12"><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>
	</channel>
</rss>

