Clipmarks
enbarfollowshare
2-25-2007 1:44 AM
371 views
enbar says:
Via Digistrom (http://snurl.com/1b9j2).
4 Comments   | Add a Comment
3-18-2007 1:23 AM
Xtraeme
Enbar, if you're familiar with Perl & regular expressions, you might want to give this a go:

($origregexp = shift @ARGV) || die <<"HELPMESSAGE";
Usage: rename perlexpr [filenames]

Example: rn.pl "s/(.*?)\\.(.*\$)/\${1}\\q\\.\${2}/" *;
s/FILENA.EXTEN/FILE\\q.EXTEN/

Notes:
\\q acts as a built in incrementer. So if you have series
of files and the goal is to number them, you could use
a Perl expression to pass the arguments in, in a sorted
manner.

HELPMESSAGE

if (!@ARGV) {
@ARGV = <STDIN>;
chomp(@ARGV);
}

$iter = 0;
$regexp = $origregexp;

#Force it to glob each entry
foreach $_ (<@ARGV> )
{
...
3-18-2007 2:51 PM
enbar
Actually, I have to admit, I have no clue about Perl or regular expressions. What you just posted is a little intimidating...
3-18-2007 9:55 PM
Xtraeme
It's not as bad as it looks. You can still use the script even if you're not familiar with Perl. Just copy the text out to a file, rename it to something like rename.pl, download a Perl interpreter - I recommend ActivePerl, and then you'll need just need a passing familiarity with regular expressions (really it's not that hard to learn).

Here are some pretty common regular expressions I use with the script.

Say I have three files Program.txt, Program.exe, and Program.MD5. Now I want rename them from Program to AnotherProgram. To do this I'd open a comm...
3-18-2007 9:56 PM
Xtraeme
Ahem ...

It uses the pattern (.*?)\. and it scans all the way across the string until it sees the period. Then everything to the left of the period is stored away and can be accessed later by referencing ${1}. Meaning ${1} will contain
Program
The dollar sign
$
means match the end of the string.

So the full expression
(.*?)\.(.*$)
simply means match ALL character up till the period in the filename and store them in ${1}. Then after the period store ALL characters up till the end of the string in ${2}. Or in other words, save the filename to ${1} and the extension to ${2}. So the string we were using as an example - Program.txt - will result ...
Login to Comment.  Not a member yet? Sign up
New from the makers of Clipmarks:  Amplify.com - Don't just share the news...Amplify it!

OK