<?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>killianfaughnan.com &#187; file</title>
	<atom:link href="http://killianfaughnan.com/tag/file/feed/" rel="self" type="application/rss+xml" />
	<link>http://killianfaughnan.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 23 Feb 2011 09:05:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Renaming Files Using Perl</title>
		<link>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/</link>
		<comments>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 13:30:07 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[rename]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=188</guid>
		<description><![CDATA[I finally got around to writing a script to rename all my music files to the same convention. That being either &#60;track&#62;-&#60;song&#62;.mp3 or &#60;song&#62;.mp3 with no spaces (I prefer underscores instead) and all lowercase. I know, it looks so simple! But the problem is all those occasions where you have something like &#60;track&#62;_-_&#60;SONG&#62;_-_(&#60;remix by&#62;).mp3 which [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got around to writing a script to rename all my music files to the same convention. That being either &lt;track&gt;-&lt;song&gt;.mp3 or &lt;song&gt;.mp3  with no spaces (I prefer underscores instead) and all lowercase. I know, it looks so simple! But the problem is all those occasions where you have something like &lt;track&gt;_-_&lt;SONG&gt;_-_(&lt;remix by&gt;).mp3 which just looks horrible! What this script does could be done in a lot less lines than I have here, but I thought it might be a good way for someone who is unsure of regular expressions to get a grasp on the topic.</p>
<p>So heres the script:</p>
<pre>#!/usr/bin/perl -w
use strict;

# Open 'find' process to list files recursively with paths
open(FIND, "find |");
while(&lt;FIND&gt;) {
 # remove leading / trailing whitespace
 chomp; 

 # Don't rename ourself
 next if $_ eq $0;       

 # create temp file (windows wont allow to rename in place from uppercase to lowercase)
 my $name = $_;
 my $tmp = $_.'~';
 rename($name, $tmp);

 # make lowercase
 $name = lc($name);
 rename($tmp, $name);

 my $newname = $name;
 # remove apostrophes
 $newname =~ s/[']//g;
 # remove round brackets and replace with hyphens
 $newname =~ s/[()]/-/g;
 # remove spaces and replace with underscores
 $newname =~ s/ /_/g;
 # remove where in sequence there is underscore, hyphen, underscore and replace with a hyphen
 $newname =~ s/_-/-/g;
 $newname =~ s/-_/-/g;
 # where there are one or more digits followed by an underscore change the underscore to a hyphen
 $newname =~ s/(d+)_/$1-/g;
 # remove all ampersands and replace them with '_and_'
 $newname =~ s/&amp;/_and_/g;
 # remove underscores where there are 2 or more, and replace with a single underscore
 $newname =~ s/_{2,}/_/g;

 # write out the changes
 rename($name,$newname);

}
close(FIND);</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

