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?
On Fri, Aug 7, 2009 at 10:32 PM, Jacky
Cheung[email protected] 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?
One possible method to map the file names:
irb(main):001:0> a = %w{ test01 test02 test03 test09 test10 test11 }
=> [“test01”, “test02”, “test03”, “test09”, “test10”, “test11”]
irb(main):002:0> b = a.map{|i| i.scan(/([^\d]+)(\d+)/).flatten }
=> [[“test”, “01”], [“test”, “02”], [“test”, “03”], [“test”, “09”],
[“test”, “10”], [“test”, “11”]]
irb(main):003:0> c = b.map{|i| [ i[0], i[1].to_i ].join }
=> [“test1”, “test2”, “test3”, “test9”, “test10”, “test11”]
On Aug 7, 2009, at 19:32 , Jacky C. 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:
Hi,
2009/8/8 Jacky C. [email protected]:
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?
Try this:
Dir.glob(“aa/*”).each{|f|File.rename(f,f.gsub(/test0+(?=\d)/,“test”))}
Regards,
Park H.