Shuffle

Tyro[a.c.edwards] no at spam.com
Sun Jan 27 02:16:23 PST 2008


Tyro[a.c.edwards] さんは書きました:
> Walter Bright さんは書きました:
>> For fun, I ordered a new car stereo that would play music from an SD 
>> card or USB stick(rather than from CD), and installed it over the 
>> weekend. The problem is loading songs onto the SD card from my home 
>> music server, which I like to hear played at random.
>>
>> 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!
>> ----------------------------
>>
>> /* 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[]
>>      */
>>     string[] files;
>>     bool callback(DirEntry *de)
>>     {
>>     if (de.isdir)
>>         listdir(de.name, &callback); // recurse into subdirectories
>>     else
>>     {
>>         // Collect only files with mp3 and wma extensions
>>         auto ext = getExt(de.name);
>>         if (fnmatch(ext, "mp3") || fnmatch(ext, "wma"))
>>         files ~= de.name;
>>     }
>>     return true;    // keep going
>>     }
>>     std.file.listdir(fromdir, &callback);
>>
>>     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;
>> }
> 
> Appreciate the example, it actually came in quite handy. Quick question, 
> how does one modify this to copy an entire directory? Additionaly, 
> version(linux) std.file.copy() preserves the filestamps of the files 
> copied. Is there a way to build this characteristic into the windows 
> version?
> 
> Thanks

I thought there might be a way to copy the entire directory intact. I 
didn't find anyting in the library that allows this but it really isn't 
that important. I can recreat the directory structure as I go along.

Thanks.
Andrew


More information about the Digitalmars-d-announce mailing list