From: dblack@... Date: 2006-07-10T00:16:38+09:00 Subject: Re: renaming files randomly Hi -- On Mon, 10 Jul 2006, Bil Kleb wrote: > 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... 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 -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for Rails developers http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log dblack@wobblini.net => me