#!/usr/bin/perl #fill_player.pl #Fill a flash device with randomly-selected songs from your collection #Copyright Cedric Nelson, 2008 #cedric.nelson@gmail.com use File::Find; use File::Copy; use strict; my ($source_dir, $destination_dir, %music_index, @music_file_list, $music_file_count, $total_file_size, $remaining, $percent_used, $random_file_index, $random_file_size, @transfer_list, $transfer_file_count); $source_dir = '/home/cedric/Music'; ($destination_dir) = @ARGV; unless ($destination_dir) {$destination_dir = '/media/S3/Music';} #Build music file index find(\&index_files, $source_dir); while (my ($file, $size) = each %music_index) { if ($file =~ /\.mp3$/) { push @music_file_list, $file; $total_file_size += $size; } } $music_file_count = @music_file_list; ($remaining, $percent_used) = calculate_free($destination_dir); print "Source dir: $source_dir\nFile count: $music_file_count\nTotal f +ile size (KB): $total_file_size\n\n"; print "Destination dir: $destination_dir\nPercent used: $percent_used\ +nRemaining space (KB): $remaining\n\n\n"; #Generate list of randomly-selected files until ($music_file_count == 0) { $random_file_index = int(rand($music_file_count + 1)); $random_file_size = $music_index{$music_file_list[$random_file_index +]}; if ($music_file_list[$random_file_index] && $random_file_size < $rem +aining) { ##print "Selected file: $random_file_index, $music_file_list[$rand +om_file_index], $random_file_size\n"; push @transfer_list, (splice @music_file_list, $random_file_index, + 1); $remaining -= $random_file_size; ##print "Estimated remaining: $remaining\n"; $music_file_count = @music_file_list; } else { splice @music_file_list, $random_file_index, 1; $music_file_count = @music_file_list; } } print "Random playlist:\n"; for (@transfer_list) { print "$_\n"; } $transfer_file_count = @transfer_list; print "Total: $transfer_file_count\n"; print "Estimated remaining space (KB): $remaining\n\n\n"; #Copy files to media player print "Copying...\n"; print "Used\tFiles\tRemaining space (KB)\n"; until ($transfer_file_count == 0) { my $file = shift @transfer_list; $transfer_file_count = @transfer_list; copy("$file","$destination_dir/$_") or warn "Copy failed: $!"; ($remaining, $percent_used) = calculate_free($destination_dir); print "$percent_used\t$transfer_file_count\t$remaining\n"; } print "Done.\n"; sub calculate_free { my ($dir) = @_; my ($free_space, @free_space, $used_percent); $free_space = `df $dir`; @free_space = split /\n/, $free_space; shift @free_space; $free_space = shift @free_space; #Example output: #/dev/sdc1 3889424 1869992 2019432 49% /media/S3 @free_space = split /\s+/, $free_space; $free_space = $free_space[3]; $used_percent = $free_space[4]; #Returned value is in KB return ($free_space, $used_percent); } sub index_files { #Filesize is returned in bytes my $filesize = -s "$File::Find::name"; $filesize = int($filesize / 1024); unless ($music_index{$File::Find::name}) {$music_index{$File::Find:: +name} = $filesize;} }

In reply to Random-fill media device by colakong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.