# Renaming files randomly

**URL:** <https://www.ruby-forum.com/t/renaming-files-randomly/97683>\
**Category:** Ruby\
**Created:** [July 9, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683 "2006-07-09T17:07:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/1 "2006-07-09T17:07:49Z")

</div>

Got this request yesterday and still haven’t had  
time to decipher it and code it – I figured I’d  
throw it out here and see what you folks come up with:

Eric wrote:

> I want to change the names of several hundred files to random numbers  
> (without overwriting any files). I have a bunch of pictures for our  
> reception, but they’re basically in chronological order by their  
> current  
> filenames. But I want the slideshow to play in random order, so I  
> need to  
> randomize the filenames.

I’ll also ask why the slideshow software can’t just be  
told to show things randomly…

Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/2 "2006-07-09T17:17:10Z")

</div>

Hi –

On Mon, 10 Jul 2006, Bil K. wrote:

> > randomize the filenames.
> 
> I’ll also ask why the slideshow software can’t just be  
> told to show things randomly…

Dir[“photos/\*”].sort\_by { rand }

If the renaming is really necessary, then something like:

Dir.chdir(“photos”) do  
files = Dir["\*"]  
new\_names = [\*0…files.size].sort\_by { rand }  
files.zip(new\_names).each do |f,n|  
File.rename(f, n.to\_s)  
end  
end

They may not get shown in numerical order, because of how directories  
get sorted, but it shouldn’t matter because they’ll be in random order  
anyway.

David

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/3 "2006-07-09T17:29:30Z")

</div>

On Mon, 10 Jul 2006, Bil K. wrote:

> > randomize the filenames.
> 
> I’ll also ask why the slideshow software can’t just be  
> told to show things randomly…
> 
> Thanks,

rather than rename, i’d just make a dir full of links:

harp:~ \> cat a.rb  
require ‘fileutils’

dir, dir\_full\_of\_links, ignored = ARGV

dir\_full\_of\_links ||= dir + ‘.d.links’  
d, f, fu = Dir, File, FileUtils

glob = f.join dir, ‘\*’  
srcs = d.glob(glob).map{|path| f.expand\_path path}

link = ‘a’  
dsts = srcs.map{|path| f.join dir\_full\_of\_links, link.succ!}

fu.rm\_rf dir\_full\_of\_links  
fu.mkdir\_p dir\_full\_of\_links  
dsts.sort\_by{ rand }

srcs.zip(dsts) do |src, dst|  
puts “#{ src } --\>\> #{ dst }”  
fu.ln\_s src, dst  
end

harp:~ \> ls -ltar tmp  
total 716  
-rw-rw-r-- 1 ahoward ahoward 393 May 5 05:30  
G2006050506.sfctmp.dat.hdr  
-rw-rw-r-- 1 ahoward ahoward 393 May 5 11:30  
G2006050512.sfctmp.dat.hdr  
-rw-rw-r-- 1 ahoward ahoward 260640 May 5 11:30  
G2006050512.sfctmp.dat  
-rw-rw-r-- 1 ahoward ahoward 393 May 5 17:30  
G2006050518.sfctmp.dat.hdr  
-rw-rw-r-- 1 ahoward ahoward 260640 May 5 17:30  
G2006050518.sfctmp.dat  
-rw-rw-r-- 1 ahoward ahoward 90112 May 23 09:30  
G2006050506.sfctmp.dat  
drwxrwxr-x 2 ahoward ahoward 8192 Jul 9 09:25 .  
drwx------ 170 ahoward ahoward 81920 Jul 9 09:26 …

harp:~ \> ruby a.rb tmp  
/home/ahoward/tmp/G2006050506.sfctmp.dat --\>\> tmp.d.links/b  
/home/ahoward/tmp/G2006050506.sfctmp.dat.hdr --\>\> tmp.d.links/c  
/home/ahoward/tmp/G2006050512.sfctmp.dat --\>\> tmp.d.links/d  
/home/ahoward/tmp/G2006050512.sfctmp.dat.hdr --\>\> tmp.d.links/e  
/home/ahoward/tmp/G2006050518.sfctmp.dat --\>\> tmp.d.links/f  
/home/ahoward/tmp/G2006050518.sfctmp.dat.hdr --\>\> tmp.d.links/g

harp:~ \> ls -ltar tmp.d.links/  
total 88  
lrwxrwxrwx 1 ahoward ahoward 44 Jul 9 09:26 g -\>  
/home/ahoward/tmp/G2006050518.sfctmp.dat.hdr  
lrwxrwxrwx 1 ahoward ahoward 40 Jul 9 09:26 f -\>  
/home/ahoward/tmp/G2006050518.sfctmp.dat  
lrwxrwxrwx 1 ahoward ahoward 44 Jul 9 09:26 e -\>  
/home/ahoward/tmp/G2006050512.sfctmp.dat.hdr  
lrwxrwxrwx 1 ahoward ahoward 40 Jul 9 09:26 d -\>  
/home/ahoward/tmp/G2006050512.sfctmp.dat  
lrwxrwxrwx 1 ahoward ahoward 44 Jul 9 09:26 c -\>  
/home/ahoward/tmp/G2006050506.sfctmp.dat.hdr  
lrwxrwxrwx 1 ahoward ahoward 40 Jul 9 09:26 b -\>  
/home/ahoward/tmp/G2006050506.sfctmp.dat  
drwx------ 171 ahoward ahoward 81920 Jul 9 09:26 …  
drwxrwxr-x 2 ahoward ahoward 4096 Jul 9 09:26 .

regards.

-a

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/4 "2006-07-09T17:42:05Z")

</div>

On Mon, 10 Jul 2006 removed\_email\_address@domain.invalid wrote:

> then the renaming order is
> 
> at the end of the loop you’ll have only files ‘a’ and ‘b’ and no error will  
> have been thrown.

sorry - replace ‘a b c’ with ‘1 2 3’ and ‘c b a’ with ‘3 2 1’ -  
basically you  
lose data when the slides have numbered names to begin with, which i  
noticed  
because this is exactly how my slideshow software names it’s slides!  
😉

-a

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/5 "2006-07-09T17:35:51Z")

</div>

On Mon, 10 Jul 2006 removed\_email\_address@domain.invalid wrote:

> end  
> this loop loses data

if

files = %w( a b c )

and

new\_names = %w( c b a )

then the renaming order is

a --\>\> c  
b --\>\> b  
c --\>\> a

so

File.rename ‘a’, ‘c’ # c is clobbered  
File.rename ‘b’, ‘b’ # ok  
File.rename ‘c’, ‘a’ # a is clobbered, new c is lost

at the end of the loop you’ll have only files ‘a’ and ‘b’ and no error  
will  
have been thrown.

regards.

-a

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/6 "2006-07-09T18:07:27Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Dir.chdir(“photos”) do  
> files = Dir["\*"]  
> new\_names = [\*0…files.size].sort\_by { rand }  
> files.zip(new\_names).each do |f,n|  
> File.rename(f, n.to\_s)  
> end  
> end

Thanks David.

I’ll push that over and see what comes back.

Also, thanks for showing “zip” in action – I just  
learned about that one a couple months ago and hadn’t  
figured out a place to use it yet.

Regards,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/7 "2006-07-09T17:51:21Z")

</div>

Hi –

On Mon, 10 Jul 2006, removed\_email\_address@domain.invalid wrote:

> > new\_names = %w( c b a )  
> > File.rename ‘b’, ‘b’ # ok  
> > File.rename ‘c’, ‘a’ # a is clobbered, new c is lost
> > 
> > at the end of the loop you’ll have only files ‘a’ and ‘b’ and no error will  
> > have been thrown.
> 
> sorry - replace ‘a b c’ with ‘1 2 3’ and ‘c b a’ with ‘3 2 1’ - basically you  
> lose data when the slides have numbered names to begin with, which i noticed  
> because this is exactly how my slideshow software names it’s slides! 😉

OK, then:

new\_names = [\*0…files.size].map {|n| “MUNGED#{n}” }.sort\_by { rand  
}

🙂

David

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/8 "2006-07-09T20:17:24Z")

</div>

On Mon, Jul 10, 2006 at 01:05:06AM +0900, Bil K. wrote:

> Thanks David.
> 
> I’ll push that over and see what comes back.
> 
> Also, thanks for showing “zip” in action – I just  
> learned about that one a couple months ago and hadn’t  
> figured out a place to use it yet.

One useful use of zip is transformation two arrays into hash pairs:

> Hash[\*%w(a b c d).zip([1, 2, 3, 4]).flatten]  
> =\> {“a”=\>1, “b”=\>2, “c”=\>3, “d”=\>4}

marcel

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [July 9, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/renaming-files-randomly/97683/9 "2006-07-09T20:17:15Z")

</div>

Hey,

You could rewrite without the zip and the temporary variable…  
ala:

Dir.chdir(“photos”) do  
Dir["\*"].sort\_by{rand}.each\_with\_index do |f,n|  
File.rename(f, n.to\_s)  
end  
end

Though be careful, you’ve just lost the file-name extension, and  
unpretty things could happen if you run the script over the same  
directory multiple times (which I guess could happen when you want to  
re-shuffle the playlist).

Regards,

Tris
