Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Obligatory perl Reply' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Obligatory perl Reply
Authored by: readparse on Mar 15, '05 12:23:55AM
ls *.rtf | perl -MFile::Copy -lne '($new = $_) =~ s/\.rtf$/\.ext/i;move($_, $new)'

-MFile::Copy - this tells perl to use the File::Copy module (which provides the move() function) -- this is a built-in module

-lne - this is the standard way to call perl on the command line when you pipe output to it and you want it to handle newlines in the expected way. Then you give it the perl code in quotes

This next line is kind of weird looking but it's action-packed... it both assigns the value of $_ to the new variable, $new, and then does a search and replace in the next variable's value. It's searching for ".rtf" at the end of the string (case-insensitive, says the "i" after the slash), and replaces it with the new extention (".ext" in this case).

($new = $_) =~ s/\.rtf$/\.ext/i;

Now we have the old filename ($_, the default variable for the current line of input, and $new, the new and updated filename), so we just call the move() function provided by the File::Copy module.

RP

[ Reply to This | # ]