Shuffle

eao197 eao197 at intervale.ru
Thu Jan 24 22:50:05 PST 2008


On Fri, 25 Jan 2008 07:35:04 +0300, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> The solution is a simple D program, shuffle, which will randomly copy  
> music files to an SD card until it fills up. Have some fun with it!

I think than the code could be yet more compact, simple and script-like if  
std.file (or std.path) module will contain something like Dir.glob from  
Ruby (http://www.ruby-doc.org/core/classes/Dir.html#M002347)

> /* Program to randomly copy music files from source to destination  
> device.
>   * Written in the D programming language.
>   * Written by Walter Bright, http://www.digitalmars.com
>   * Placed into the Public Domain.
>   */
>
>
> import std.file;
> import std.stdio;
> import std.string;
> import std.c.stdlib;
> import std.path;
> import std.random;
>
> int main(string[] args)
> {
>      if (args.length != 3)
>      {	writefln("Usage: shuffle fromdir todir");
> 	exit(1);
>      }
>      auto fromdir = args[1];
>      auto todir = args[2];
>
>      /* Recursively search for all the mp3 and wma files in directory  
> fromdir
>       * and put them into files[]
>       */
       auto files = std.file.glob( std.path.join( fromdir, "**/*.{mp3,wma}"  
) );
>
>      writefln(files.length, " music files");
>
>      /* The loop will normally quit via an exception when the target  
> device
>       * is full. But if there are not enough files in the source to fill
>       * up the target, the loop ensures it will still eventually quit.
>       */
>      for (size_t i = 0; i < files.length; i++)
>      {
> 	auto j = std.random.rand() % files.length;
> 	auto fromfile = files[j];
> 	auto tofile = std.path.join(todir, basename(fromfile));
> 	writefln("%s => %s", fromfile, tofile);
> 	std.file.copy(fromfile, tofile);
>      }
>
>      writefln("Done");
>      return 0;
> }



-- 
Regards,
Yauheni Akhotnikau


More information about the Digitalmars-d-announce mailing list