Maybe this doesn’t come under code... but it’s certainly a cure for people who are kind of obsessive but lazy.
Let’s see... this is my configuration: ActivePerl with Windows XP Pro. [Yes this matters for this example].
I got a directory of MP3's by Too Kool Chris, All Named "Too KC - Tr No #". Now I kind of wanted all the names to be "Too Kool Chris - Track Number #". If I did that by hand, btw there were 50 files, it would be quite tiring. Here’s where a bit of simple code comes in.
Did I mention I'm lazy...? I didn’t have the original files so I used this to create them:
#!c:/perl/bin/perl
# Initialize variable i to 1
$i = 1;
# Start the loop
while ($i < 51)
{
# Set var tr
$tr = "Too KC - Tr No ";
# Concatenate previous tr to the number
$tr .= $i;
# Concatenate to add .mp3 to the end
$tr .= ".mp3";
# Create the file, this is actually open and create if not exist
open(FILE,">$tr");
# must close.. otherwise it’s BAD
close(FILE);
# increment i
$i++;
}
###############
# END OF CODE #
###############
Wasn't that easy.. Now we want to rename it.. Here’s the code we would use, You would have to customize it every time because it’s not the same every time and on every system.
#!c:/perl/bin/perl
# `` executes a system command
# this will put output of the dir command into array @dir
@dir = `dir`;
# start loop and store current element in $line
foreach $line (@dir)
{
# Split by spaces, since files don’t really
# have any attributes, I have to set the max
# splits to 4 to make sure it doesn’t split
# the filename The s+ is to Turn all multiple
# spaces to single spaces.
@temp = split(/s+/,$line,4);
# We are only interested in the 4th
# element as that contains the filename.
$name = $temp[3];
# Remove trailing new lines
$name =~ s/ +//;
# Only get names which include Too KC so we only
# renames those ;]
if ($name =~ m/Too KC/)
{
# Get the number of the file
@tmp = split(/ /,$name);
$num = $tmp[5];
# Rename the file
# Notice " is written as ", is the
# escape character, It makes sure that "
# is not interpreted as double quotes
# which are to be evaluated which might
# confuse the compiler and most probably
# (too lazy to check), give you an error
# when you try to run it ;P
`ren "$name" "Too Kool Chris - Track Number $num"`;
# Print confirmation, rem if it will
# irritate you ;]
print "$name renamed to Too Kool Chris - Track Number $num ";
}
# End Loop
}
###############
# END OF CODE #
###############
There you go.. You have a full directory of properly named files, and writing that code was much more fun than renaming it manually ;] This is a VERY simple example, You can make it do very complex things. People in this world are very lazy, and this will just turn that boredom into something interesting. Try it with
something. I've used almost the same code to rename all my mp3's so that the first letter of each word is capital... Now if I tried that by hand I wouldn’t really have time to write this would I ?
If you have any questions, come to irc.cyberarmy.com on #OSI ;]
~ shn ~
This article was originally written by shn |