File Shuffler
This is a small Perl script to shuffle files. By that, I mean take a directory of files and put a unique random number before (as a prefix to) each file. When the files in the directory are listed alphabetically afterwards, they will actually be listed randomly.
I had need of this because the MP3 player function in my car stereo plays the files it find on the CD alphabetically. I wanted to randomize/shuffle the songs, and the workaround was to use this script on the files before burning the CD.
Here is the code:
#!/usr/bin/perl -w use strict; use List::Util qw/shuffle/; my $directory = shift or die "Usage: $0 <directory>\n"; chdir $directory or die "Error: Could not change to directory.\n"; my @files = glob "*"; # Find digits in amount of files. my $digits = 0; for (split //, scalar(@files)) { $digits++; } for my $number (&shuffle(1..scalar(@files))) { my $file = shift @files; my $new = sprintf "%0${digits}d--%s", $number, $file; rename $file, $new; }