<?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>Southern Bread &#187; perl</title>
	<atom:link href="http://www.southernbread.org/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.southernbread.org</link>
	<description>Southern History, American Freedom, Christian Liberty</description>
	<lastBuildDate>Sat, 04 Feb 2012 21:12:04 +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>NumPad &#8211; Pad Numbers in Filenames</title>
		<link>http://www.southernbread.org/numpad-pad-numbers-in-filenames/</link>
		<comments>http://www.southernbread.org/numpad-pad-numbers-in-filenames/#comments</comments>
		<pubDate>Fri, 22 Sep 2006 20:33:00 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.southernbread.org/software/numpad.html</guid>
		<description><![CDATA[One of the most annoying things with mp3&#8217;s (other than DRM of course) is consecutive numbering. The sort algorithm on Windows and most mp3 players sorts as it goes through the filename one character at a time. So if you have 12 files named &#8220;file-1.mp3 &#8230; file-12.mp3&#8243;, they will sort like this: file-1.mp3 file-10.mp3 file-11.mp3 [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most annoying things with mp3&#8217;s (other than DRM of course) is consecutive numbering.  The sort algorithm on Windows and most mp3 players sorts as it goes through the filename one character at a time.  So if you have 12 files named &#8220;file-1.mp3 &#8230; file-12.mp3&#8243;, they will sort like this:</p>
<div class="code"><pre><pre>
file-1.mp3
file-10.mp3
file-11.mp3
file-12.mp3
file-2.mp3
file-3.mp3
file-4.mp3
file-5.mp3
file-6.mp3
file-7.mp3
file-8.mp3
file-9.mp3
</pre></pre></div>
<p>That is obviously not what anybody wants.  To correct this, consecutively numbered files must be zero-padded in order to sort correctly.  So the previous list would need to look like this:</p>
<div class="code"><pre><pre>
file-01.mp3
file-02.mp3
file-03.mp3
file-04.mp3
file-05.mp3
file-06.mp3
file-07.mp3
file-08.mp3
file-09.mp3
file-10.mp3
file-11.mp3
file-12.mp3
</pre></pre></div>
<p>I run into this problem often enough that I started looking around for software that would pad the numbers in filenames automatically for me.  I couldn&#8217;t find anything out there that would do it in an automated fashion so I wrote a perl script to do it.  I wrote it in Perl so that I could run it under Linux as well.  I frequently need to have jpeg&#8217;s consecutively numbered as well so that was important.  Here is the script:</p>
<div class="code"><pre><pre>
#!/usr/bin/perl

use strict;
use warnings;

my $dirname = $ARGV[0] || &quot;&quot;;
my $change&nbsp;&nbsp;= $ARGV[1] || &quot;no&quot;;
my $ext&nbsp;&nbsp;&nbsp;&nbsp; = $ARGV[2] || &quot;.mp3&quot;;
my $padsize = $ARGV[3] || 4;

##: Give usage if no arguments are defined
if ($dirname eq &quot;&quot;) {
&nbsp;&nbsp;print STDOUT &quot;Usage: numpad.pl \&quot;directory\&quot; [change?] [.ext] [padsize]\n&quot;;
&nbsp;&nbsp;print STDOUT &quot;&nbsp;&nbsp;[change?] - default is \&quot;no\&quot; - should I actually rename the file\n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;print STDOUT &quot;&nbsp;&nbsp;[.ext] - default is \&quot;.mp3\&quot; - what files should be acted upon\n&quot;;
&nbsp;&nbsp;print STDOUT &quot;&nbsp;&nbsp;[padsize] - default is \&quot;4\&quot; - how many digits should I pad to\n&quot;;
&nbsp;&nbsp;print STDOUT &quot;\n&quot;;
&nbsp;&nbsp;print STDOUT &quot;Example:\n&quot;;
&nbsp;&nbsp;print STDOUT &quot;&nbsp;&nbsp;numpad.pl \&quot;c:\\mp3\\album\&quot; yes .wav 5\n&quot;;
&nbsp;&nbsp;exit(1);
}

##: Change to the directory asked for
chdir($dirname) or die &quot;Couldn&amp;#8217;t change to directory &amp;#8217;$dirname&amp;#8217;: $!&quot;;

##: Open and read in the directory based on the fileglob given
opendir(DIR, &quot;$dirname&quot;) or die &quot;Couldn&amp;#8217;t open directory &amp;#8217;$dirname&amp;#8217;: $!&quot;;
my @files = grep { /.*$ext/ } readdir(DIR);
closedir DIR;

##: Loop through the file entries and mod them
foreach my $file (@files) {
&nbsp;&nbsp;my $newfilename=&quot;&quot;;

&nbsp;&nbsp;##: Cut off the extension, to be put back on later
&nbsp;&nbsp;$file =~ s/$ext//g;

&nbsp;&nbsp;##: Split the filename into an array using any number as a separator
&nbsp;&nbsp;my @fnparts = split(/([0-9]*)/, $file);

&nbsp;&nbsp;##: Loop through the array and pad any numerical elements
&nbsp;&nbsp;foreach (@fnparts) {
&nbsp;&nbsp;&nbsp;&nbsp;my $result = $_;
&nbsp;&nbsp;&nbsp;&nbsp;my $number = 0;
&nbsp;&nbsp;&nbsp;&nbsp;if($result =~ m/[0-9]+/) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$number = sprintf(&quot;%0&quot;.$padsize.&quot;d&quot;, $result);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$newfilename.=$number;
&nbsp;&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$newfilename.=$result;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;$newfilename.=$ext;
&nbsp;&nbsp;$file.=$ext;
&nbsp;&nbsp;print STDOUT &quot;$file\n&nbsp;&nbsp;-&gt; $newfilename\n&quot;;
&nbsp;&nbsp;if($change eq &quot;yes&quot;) {
&nbsp;&nbsp;&nbsp;&nbsp;rename($file, $newfilename);
&nbsp;&nbsp;}
}
</pre></pre></div>
<p>Just save this code as a file named <b>numpad.pl</b>.  If you run it on Windows, you will have to either install a perl interpereter first(get the <a href="http://www.activestate.com/store/activeperl/download/">MSI package from ActiveState</a>) or download the native executable version below that was compiled with <a href="http://www.indigostar.com">Perl2Exe</a>.  If you use the interpreter method you would need to execute perl and pass the script name to it as the first argument.  Like this:</p>
<div class="code"><pre><pre>
c:\&gt; perl numpad.pl &quot;c:\mp3\album&quot; yes .wav 5
</pre></pre></div>
<p>That example would zero-pad all the files in the <i>c:\mp3\album</i> directory that end with <i>.wav</i>, making any number it finds in a filename at least <i>5</i> digits long.  This script should handle any files you give it; not just mp3&#8217;s.  You can also re-run it with a different padding amount and it will redo the padding if you put too many on the first time.  If you tell it &#8220;no&#8221; as the second argument, it will just show you what it would do instead of actually doing it.  As always, <b>USE AT YOUR OWN RISK!</b></p>
<ul>
<li>Native Exe Version: <a href="/downloads/numpad.exe">numpad.exe</a></li>
<li>Perl Source Version: <a href="/downloads/numpad">numpad</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.southernbread.org/numpad-pad-numbers-in-filenames/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

