<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.townx.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>townx - Script to copy random mp3s from a directory to an mp3 player - Comments</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player</link>
 <description>Comments for &quot;Script to copy random mp3s from a directory to an mp3 player&quot;</description>
 <language>en</language>
<item>
 <title>reply</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-44229</link>
 <description>&lt;p&gt;there is no strategy just how nifty this unique illegal program would be :) Simply yesteryear There is a good 5 various year-old jogging shoe common box loaded with compact discs along with thousand&#039;s mp3&#039;s. I had no clue that which is presently there (good, I am aware of slightly, but that is the item). The thing is that just where Let me within this ;)&lt;br /&gt;
Thanks again  &lt;br /&gt;
&lt;a href=&quot;http://www.code-promo-coupon.com/3suisses&quot;&gt;lynel&lt;/a&gt;&lt;/p&gt;</description>
 <pubDate>Sat, 29 Dec 2012 05:52:04 -0600</pubDate>
 <dc:creator>lynel</dc:creator>
 <guid isPermaLink="false">comment 44229 at http://www.townx.org</guid>
</item>
<item>
 <title>sys-filesystem should</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-40264</link>
 <description>&lt;p&gt;sys-filesystem should include the to_kb method; if not, they may have changed the &lt;span class=&quot;caps&quot;&gt;API.&lt;/span&gt;&lt;/p&gt;</description>
 <pubDate>Mon, 18 Jan 2010 17:37:24 -0600</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 40264 at http://www.townx.org</guid>
</item>
<item>
 <title>error after finaly installing all in fedora 12 :((</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-40234</link>
 <description>&lt;p&gt;after running ruby mp3s_random.rb&lt;/p&gt;

&lt;p&gt;undefined method `to_kb&#039; for 1977987072:Bignum (NoMethodError)&lt;/p&gt;

&lt;p&gt;sudo yum install rubygems&lt;br /&gt;
sudo yum install ruby-devel&lt;br /&gt;
sudo rake install&lt;br /&gt;
gem install sys-filesystem&lt;/p&gt;</description>
 <pubDate>Tue, 05 Jan 2010 06:55:28 -0600</pubDate>
 <dc:creator>remke</dc:creator>
 <guid isPermaLink="false">comment 40234 at http://www.townx.org</guid>
</item>
<item>
 <title>This is so cool!</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-39859</link>
 <description>&lt;p&gt;My husband has been teaching me about programming, he has recommend me to read this article and try to make it work on my &lt;span class=&quot;caps&quot;&gt;MP2&lt;/span&gt; Player.&lt;/p&gt;

&lt;p&gt;This is a great project for me, fun fun fun!&lt;/p&gt;</description>
 <pubDate>Thu, 03 Sep 2009 14:24:09 -0500</pubDate>
 <dc:creator>Chicago Realtor</dc:creator>
 <guid isPermaLink="false">comment 39859 at http://www.townx.org</guid>
</item>
<item>
 <title>Man, I wish I knew how to code</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-39712</link>
 <description>&lt;p&gt;After reading this entry about your nifty script, I am filed with &lt;span class=&quot;caps&quot;&gt;ENVY. &lt;/span&gt; I wish I know how to code.  Some stuff that I would code:&lt;/p&gt;

&lt;p&gt;If I paste a web page onto a form the form automatically takes out the links and spits out a nice clickable list&lt;/p&gt;

&lt;p&gt;A script that takes all files from all folders and spits out a nice list&lt;/p&gt;

&lt;p&gt;Anyway, enough of my daydreaming.  Do you think it&#039;s possible to learn how to code just by reading webpages online?  Are there any free tutorials?&lt;/p&gt;</description>
 <pubDate>Sat, 11 Jul 2009 19:24:41 -0500</pubDate>
 <dc:creator>Advice for Women</dc:creator>
 <guid isPermaLink="false">comment 39712 at http://www.townx.org</guid>
</item>
<item>
 <title>Haha, you have no idea how</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comment-39697</link>
 <description>&lt;p&gt;Haha, you have no idea how handy this script will be :) Just yesterday I&#039;ve found a 5 year old shoe box full of cd&#039;s with thousand&#039;s mp3&#039;s. I&#039;ve no idea what is there (well, I know  a bit, but that&#039;s it). You see where I&#039;m going with this ;)&lt;br /&gt;
Thanks again!&lt;/p&gt;</description>
 <pubDate>Tue, 07 Jul 2009 07:39:50 -0500</pubDate>
 <dc:creator>Paznokcie</dc:creator>
 <guid isPermaLink="false">comment 39697 at http://www.townx.org</guid>
</item>
<item>
 <title>Script to copy random mp3s from a directory to an mp3 player</title>
 <link>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player</link>
 <description>&lt;p&gt;This is a Ruby script which randomly copies mp3 files from one directory to an mp3 player. I wrote it so I could easily fill up my mp3 player from the 9000 odd mp3s I have on a different external drive.&lt;/p&gt;

&lt;p&gt;To run it, you&#039;ll need the &lt;strong&gt;sys-filesystem&lt;/strong&gt; gem (see &lt;a href=&quot;http://rubyforge.org/projects/sysutils&quot; title=&quot;http://rubyforge.org/projects/sysutils&quot;&gt;http://rubyforge.org/projects/sysutils&lt;/a&gt;):&lt;/p&gt;



&lt;pre&gt;
$ gem install sys-filesystem
&lt;/pre&gt;



&lt;p&gt;Next, edit these variables in the script (near the top):&lt;br /&gt;
* &lt;code&gt;source_dir&lt;/code&gt; to the directory containing the mp3s you want to select from&lt;br /&gt;
* &lt;code&gt;dest_dir&lt;/code&gt; to the path for the directory on your mp3 player you want to copy to&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color:red&quot;&gt;Be a bit careful, as this will attempt to fill the &lt;code&gt;dest_dir&lt;/code&gt; you specify with mp3 files from &lt;code&gt;source_dir&lt;/code&gt;. You might end up filling the wrong disk up.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Then just run it with ruby from the command line:&lt;/p&gt;



&lt;pre&gt;
$ ruby mp3s_random.rb
&lt;/pre&gt;



&lt;p&gt;Note that it won&#039;t delete anything from the destination drive, and will attempt to fill all the space available. Also note that it doesn&#039;t keep trying mp3s until it finds one which will fit the last remaining space: once it tries to copy a file which won&#039;t fit, it stops. You can always run it again to see whether the next run finds a file small enough to fit.&lt;/p&gt;

&lt;p&gt;I&#039;ve only tested it on Linux, but, who knows, it might work on Windows too. (No operating-system specific commands are used in the script, as it uses Ruby for all file operations.)&lt;/p&gt;

&lt;p&gt;The code is below, but I&#039;ve attached it as well.&lt;/p&gt;



&lt;pre&gt;
require &#039;rubygems&#039;
require &#039;sys/filesystem&#039;
require &#039;ftools&#039;

source_dir = &#039;/media/disk/music&#039;
dest_dir = &#039;/media/disk-1/music&#039;

files = Dir[File.join(source_dir, &#039;/**/*.mp3&#039;)]
num_files = files.size

stat = Sys::Filesystem.stat(dest_dir)

disk_free_space_kb = (stat.blocks_free * stat.fragment_size).to_kb

files_selected = []

while disk_free_space_kb &amp;gt; 0 and num_files &amp;gt; 0 do
  # choose an mp3
  file_num_to_copy = rand(num_files - 1) + 1
  file_path = files.delete_at(file_num_to_copy)

  num_files = num_files - 1

  # work out how big file is
  file_size_kb = File.stat(file_path).size.to_kb

  # subtract from free space
  if (disk_free_space_kb - file_size_kb) &amp;gt; 0
    files_selected &amp;lt;&amp;lt; file_path
    disk_free_space_kb = disk_free_space_kb - file_size_kb
  else
    break
  end
end

files_selected.each do |f|
  copy_to_path = File.join(dest_dir, File.basename(f))
  puts &amp;quot;Copying #{f} to device&amp;quot;
  File.copy(f, copy_to_path)
end
&lt;/pre&gt;</description>
 <comments>http://www.townx.org/blog/elliot/script-copy-random-mp3s-directory-mp3-player#comments</comments>
 <category domain="http://www.townx.org/tech">tech</category>
 <category domain="http://www.townx.org/howtos">howtos</category>
 <enclosure url="http://www.townx.org/files/mp3s_random.rb" length="939" type="application/x-ruby" />
 <pubDate>Tue, 30 Jun 2009 04:24:41 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">778 at http://www.townx.org</guid>
</item>
</channel>
</rss>
