# I want a script can do this

**URL:** <https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410>\
**Category:** Ruby\
**Created:** [August 8, 2009, 4:33am UTC](https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410 "2009-08-08T04:33:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackycheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jackycheung](https://www.ruby-forum.com/u/jackycheung)\
**Post date:** [August 8, 2009, 4:33am UTC](https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410/1 "2009-08-08T04:33:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jackycheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jackycheung](https://www.ruby-forum.com/u/jackycheung)\
**Post date:** [August 8, 2009, 5:24am UTC](https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410/2 "2009-08-08T05:24:36Z")

</div>

On Fri, Aug 7, 2009 at 10:32 PM, Jacky  
Cheung[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”]

---

<div class="post-metadata">

**Author:** ![jackycheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jackycheung](https://www.ruby-forum.com/u/jackycheung)\
**Post date:** [August 8, 2009, 5:40am UTC](https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410/3 "2009-08-08T05:40:24Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![jackycheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jackycheung](https://www.ruby-forum.com/u/jackycheung)\
**Post date:** [August 8, 2009, 5:45am UTC](https://www.ruby-forum.com/t/i-want-a-script-can-do-this/173410/4 "2009-08-08T05:45:05Z")

</div>

Hi,

2009/8/8 Jacky C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.
