26290 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Lego X-wing fighter
touches down in New
York"s Times Square
Experts: Network
security
deteriorating,
privacy a lost
cause
Internet cafés
declared "illegal
businesses" in Ohio
SAP shuffles execs
to chase cloud
success
AT&T adds 61¢
"Mobility
Administrative Fee"
for users
Microsoft caves to
Google, pulls
YouTube app from
WinPhone Store
Amazon expands
Appstore reach,
gives devs more
user data
Now it gets
serious: Fracking
could RUIN BEER
Reports: New Xbox
could DOOM
second-hand games
market
Industry execs:
Network admins an
endangered species
Slashdot
AMD Launches New
Mobile APU Lineup,
Kabini Gets Tested
How To Hack
Twitter"s
Two-Factor
Authentication
African Soil Mapped
For the Very First
Time
BeagleBone Black
Ships With New
Linux 3.8 Kernel
Google Releases
Glass Factory
System Image,
Rooted Bootloader
White House: Use
Metric If You Want,
We Don"t Care
A Snapshot of the
Inside of an Atom
WHO: Intellectual
Property Claims
Hindering Research
On Deadly Novel
Coronavirus
Google Plans
Wireless Networks
In Emerging Markets
Intel Claims
Haswell
Architecture Offers
50% Longer Battery
Life vs. Ivy Bridge
Article viewer

How to make code do your work for you



Written by:dimport
Published by:NeorageX
Published on:2003-07-06 20:11:51
Topic:Perl
Search OSI about Perl.More articles by dimport.
 viewed 6485 times send this article printer friendly

Digg this!
    Rate this article :
A small and lazy aproach to a bit of perl using system commands ;]

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

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features


     
Your Ad Here
 
Copyright Open Source Institute, 2006