<?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>nedos.net &#187; nerd</title>
	<atom:link href="http://nedos.net/tag/nerd/feed/" rel="self" type="application/rss+xml" />
	<link>http://nedos.net</link>
	<description>Dmitry Nedospasov&#039;s Blog</description>
	<lastBuildDate>Tue, 25 May 2010 00:37:50 +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>Ruby Batch Rename Script</title>
		<link>http://nedos.net/2009/02/20/ruby-batch-rename-script/</link>
		<comments>http://nedos.net/2009/02/20/ruby-batch-rename-script/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 23:26:03 +0000</pubDate>
		<dc:creator>Dmitry Nedospasov</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[nerd]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nedos.net/?p=94</guid>
		<description><![CDATA[So I&#8217;ve recently gotten into scripting in ruby and with a couple of scripts you really can save yourself a lot of tedious work in the future. Here&#8217;s one i wrote that does a batch rename of all files in a given folder and its subfolders. You can do either of the following:
Enter 3 parameters:

input [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve recently gotten into scripting in ruby and with a couple of scripts you really can save yourself a lot of tedious work in the future. Here&#8217;s one i wrote that does a batch rename of all files in a given folder and its subfolders. You can do either of the following:</p>
<p>Enter 3 parameters:</p>
<ol>
<li>input directory</li>
<li>search string</li>
<li>replacement string</li>
</ol>
<div>Or just enter 2 parameters in which case the search string will be removed from all files:</div>
<div>
<ol>
<li>input directory</li>
<li>search string</li>
</ol>
<div>Remember, to enter the parameters in that order, &lt;input dir&gt; &lt;search string&gt; [&lt;replacement string&gt;].</div>
</div>
<div><code><br />
#!/usr/bin/ruby<br />
# Dmitry Nedospasov<br />
# Batch Rename<br />
# 2 parameters: batchrename.rb &lt;input dir&gt; &lt;search string&gt;<br />
# &lt;search string&gt; will be completely removed from all occurances<br />
# 3 parameters: batrename.rb &lt;input dir&gt; &lt;search string&gt; &lt;replacement string&gt;<br />
# all occurances of &lt;search string&gt; will be replaced with &lt;replacement string&gt;</code></div>
<div><code><br />
require 'ftools'<br />
def parseargs(arguments, stdin)<br />
# puts arguments<br />
dir = arguments[0]<br />
oldExt = arguments[1]<br />
newExt = arguments[2]<br />
puts dir<br />
puts oldExt<br />
puts newExt<br />
if oldExt == nil or dir == nil<br />
puts 'Please enter atleast the first 2 parameters: '<br />
puts ' &lt;input directory&gt; &lt;search string&gt; [&lt;replacement string&gt;]'<br />
else<br />
convertFromTo(dir,oldExt,newExt)<br />
end<br />
end<br />
def convertFromTo(dir, oldExt, newExt)<br />
files = Dir.entries(dir)<br />
files.each do |f|<br />
next if f == '.' or f == '..'<br />
fullPath = dir + '/' + f<br />
# fullPath = fullPath.gsub(' ','\ ')<br />
if File.directory?(fullPath)<br />
puts 'Found directory: ' + fullPath<br />
convertFromTo(fullPath, oldExt, newExt)<br />
else<br />
if newExt == nil<br />
newName = fullPath.gsub(oldExt,'')<br />
else<br />
newName = fullPath.gsub(oldExt,newExt)<br />
end<br />
if fullPath != newName<br />
puts fullPath + ' -&gt; ' + newName<br />
File.rename(fullPath,newName)<br />
end<br />
end<br />
end<br />
end</code></div>
<div>
<p>parseargs(ARGV, STDIN)</p>
</div>
<div>and what you should get is something like this</div>
<div><code><br />
russo@guevara:~/Desktop$ ./batchrename.rb . stpuid stupid<br />
.<br />
stpuid<br />
stupid<br />
Found directory: ./stupidtypo<br />
./stupidtypo/stpuidtypo1.txt -&gt; ./stupidtypo/stupidtypo1.txt<br />
./stupidtypo/stpuidtypo10.txt -&gt; ./stupidtypo/stupidtypo10.txt<br />
./stupidtypo/stpuidtypo2.txt -&gt; ./stupidtypo/stupidtypo2.txt<br />
./stupidtypo/stpuidtypo3.txt -&gt; ./stupidtypo/stupidtypo3.txt<br />
./stupidtypo/stpuidtypo4.txt -&gt; ./stupidtypo/stupidtypo4.txt<br />
./stupidtypo/stpuidtypo5.txt -&gt; ./stupidtypo/stupidtypo5.txt<br />
./stupidtypo/stpuidtypo6.txt -&gt; ./stupidtypo/stupidtypo6.txt<br />
./stupidtypo/stpuidtypo7.txt -&gt; ./stupidtypo/stupidtypo7.txt<br />
./stupidtypo/stpuidtypo8.txt -&gt; ./stupidtypo/stupidtypo8.txt<br />
./stupidtypo/stpuidtypo9.txt -&gt; ./stupidtypo/stupidtypo9.txt<br />
</code></div>
<div>Enjoy!!! and here&#8217;s the <a href="http://nedos.net/ruby/batchrename.rb">file for download</a></div>
]]></content:encoded>
			<wfw:commentRss>http://nedos.net/2009/02/20/ruby-batch-rename-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
