From: Ryan Davis Date: 2009-08-08T12:36:30+09:00 Subject: Re: I want a script can do this On Aug 7, 2009, at 19:32 , Jacky Cheung wrote: > I want a script can do this: > a directory aa , under aa has many files. i want batch rename all > files. > like this > the file name is test01 test02 test03 ... test09 test10 test11... > test29.... > rename > test1 test2 test3 ....test9 test10 test29..... > > how can i write this script? Below is an equivalent perl script that I've used for years and years. It should be exceedingly simple to convert to ruby (or just use it as- is) and/or simplify for your requirements. You'd use it like so: > % rename 's/(\D)0+(\d)/$1$2/' aa/* Here ya go: > #!/usr/bin/perl -w > > # Usage: rename perlexpr [files] > > ($op = shift) || die "Usage: rename perlexpr [filenames]\n"; > if (!@ARGV) { > @ARGV = ; > chop(@ARGV); > } > for (@ARGV) { > $was = $_; > eval $op; > die $@ if $@; > rename($was,$_) unless $was eq $_; > }