This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
simple-audiotools/fixname

73 lines
2.0 KiB
Perl
Executable File

#!/usr/bin/perl
#
# File Name Fixer
# usage: fixname <files>
#
# Renames files to comply with Anna's arbitrary standard
#
# This is Anna's standard:
# 1) all letters are lowercase
# 2) Whitespace becomes underscore
# 3) Never more than one consecutive underscore
# 4) No underscores flanking dashes
# 4) Never more than one consecutive dash
$VERSION = "git";
$HELP_MSG = "# Filename fixer v$VERSION\n\tusage: fixname <files>\n\t fixname -o \"name\"\n";
die $HELP_MSG if ($#ARGV < 0);
foreach (@ARGV)
{
die $HELP_MSG if (/^(--help)|(-h)$/);
if (/^-o$/)
{
$toStdOut = true;
}
if (/^-i$/)
{
$fromStdIn = true;
}
}
@ARGV = grep(!/^-o$/, @ARGV) if ($toStdOut);
@ARGV = grep(!/^-i$/, @ARGV) if ($fromStdIn);
if ($fromStdIn) { @names = <STDIN>; }
else { @names = @ARGV; }
foreach (@ARGV)
{
my $oldname = $_;
s/(.*)/lc($1)/e; # Lowercase it
s/[()\[\]{}!@#\$%^*,;:\'~]//g; # Remove odd characters that could break bash
s/[&]/and/g; # & to 'and'
s/\s/_/g; # Change whitespace to _
s/_+/_/g; # Squash multiple spaces
s/\.+/\./g; # Squash multiple dots
s/^_*//; # Remove leading space
s/_*$//; # Remove trailing space
s/_*(-|\.)_*/$1/g; # Remove space flanking a - or .
if ($toStdOut) { print; }
else { system('mv', $oldname, $_); }
}
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA