<?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>No Znx! &#187; powershell</title>
	<atom:link href="http://znx.no/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Fri, 21 May 2010 16:05:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Renaming Files In A Directory</title>
		<link>http://znx.no/2009/09/renaming-files/</link>
		<comments>http://znx.no/2009/09/renaming-files/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:58:32 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ms]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=159</guid>
		<description><![CDATA[So I had a problem on Windows in that I have lots of directories that are named correctly but I wish the files under them to be uniformly named. The solution was to use PowerShell, which is an extremely effective scripting tool for Windows, especially if you come from a UNIX shell background. There is [...]]]></description>
			<content:encoded><![CDATA[<p>So I had a problem on Windows in that I have lots of directories that are named correctly but I wish the files under them to be uniformly named.</p>
<p>The solution was to use <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">PowerShell</a>, which is an extremely effective scripting tool for Windows, especially if you come from a UNIX shell background.</p>
<p>There is actually quite a lot of documentation available for it and rather than go into detail I will simply show you the script:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#</span>
<span style="color: #008000;"># RenameDirectoryFiles.ps1</span>
<span style="color: #008000;">#</span>
&nbsp;
<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$paths</span> <span style="color: #000000;">&#41;</span>
<span style="color: #008080; font-weight: bold;">Set-PSDebug</span> <span style="color: #008080; font-style: italic;">-Strict</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$path</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$paths</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: pink;">!</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$path</span> <span style="color: #008080; font-style: italic;">-PathType</span> Container<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-weight: bold;">Write-Error</span> <span style="color: #800000;">&quot;'$path' doesn't exist or isn't a directory&quot;</span>
        exit <span style="color: #000000;">1</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
    <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800080;">$path</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">sort</span> FullName <span style="color: pink;">|</span> <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;{0:000}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$i</span>
        <span style="color: #800080;">$ext</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Extension
        <span style="color: #800080;">$newname</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$path - $tmp$ext&quot;</span>
        <span style="color: #008080; font-weight: bold;">Rename-Item</span> <span style="color: #000080;">$_</span>.fullname <span style="color: #800080;">$newname</span>
        <span style="color: #800080;">$newname</span>
        <span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #800080;">$i</span> <span style="color: pink;">+</span> <span style="color: #000000;">1</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So the usage is extremely simple:</p>
<pre>RenameDirectoryFiles.ps1 'My Directory'</pre>
<p>After which all the files under &#8216;My Directory&#8217; will be renamed to &#8216;My Directory &#8211; 000.fileextension&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/09/renaming-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
