RePost: Help to convert a code to D

Andre Artus andre.artus at gmail.com
Mon Aug 26 16:26:29 PDT 2013


On Monday, 26 August 2013 at 13:30:38 UTC, Alexandre wrote:
> Hi :)
>
> I'm starting with D language... and, I try to convert a C# code
> to D...
> So, my C# code, I get file informations, and, I create folders
> with all days of month of specific year...
>
> So, that is my C# code...
>
-- SNIP --

> So, to create directory and move files... I get the 
> documentation
> on that page: http://dlang.org/phobos/std_file.html
>
> But, I don't know, How I can get the file information and what 
> is
> the best way to create the directory's, so, I need some help :)
>
> I appreciate the help :)

Okay, I banged together something that may be close to what you 
want. I'm not a D expert, so someone is sure to point out some 
areas for improvement.

There does not seem to be any reason to use objects/classes for 
what you want. In fact if I was to write it in C# I would make 
the methods static.

As an aside: it's not generally considered good practice to do 
expensive computations or IO in a constructor (BOCTAOE).

I did not split building the archive directory structure from the 
file moving part. I prefer to only create a directory if there is 
going to be a file in it. This may have a impact on performance, 
but I suspect it is negligible. If it's an issue measure, 
measure, measure.


module main;

import std.stdio, std.algorithm, std.conv;
import std.array, std.random, std.datetime;
import std.file, std.path, std.utf, std.string;


int main(string[] argv)
{
   // string prefixed with 'r' similar to '@' in C#
   auto searchPath = r"G:\archivetest\search";
   auto archivePath = r"G:\archivetest\archive";

   moveToArchive(searchPath, archivePath);

   return 0;
}

void moveToArchive(string searchPath, string archivePath)
{
   // This ought to be a library thing.
   immutable string[12] MesesDoAno =
   [
     "Janeiro", "Fevereiro",
     "Marco", "Abril",
     "Maio", "Junho",
     "Julho", "Agosto",
     "Setembro", "Outubro",
     "Novembro", "Dezembro"
   ];

   // http://dlang.org/phobos/std_file.html#.dirEntries
   auto de = dirEntries(searchPath,"*.RE{M,T}", SpanMode.shallow, 
false);

   // Sorting not required, just a personal preference
   auto sortedFiles =
     de.array.sort!((DirEntry x, DirEntry y)
       => x.timeLastModified < y.timeLastModified);

   foreach(DirEntry e; sortedFiles) {
     // I'm being extra verbose here so that it's easy to follow 
in a debugger

     auto tlm = e.timeLastModified;

     auto _year = tlm.year;
     auto _month = tlm.month;
     auto _day = tlm.day;

     auto yearString = to!(string)(_year);
     auto monthString = MesesDoAno[tlm.month - Month.jan];

     // there ought to be a date formatting function. I can't find 
it.
     auto dayString = format("%04d-%02d-%02d", _year, _month, 
_day);

     string movePath = buildPath(archivePath, yearString, 
monthString);

     bool makeDir = !(movePath.exists && movePath.isDir);

     if(makeDir)
       mkdirRecurse(movePath);

     auto renamedFile = buildPath(movePath, e.name.baseName);

     writefln("%s -> %s", e.name, renamedFile);

     //rename(e.name, renamedFile );
     //uncomment above line if all looks good.
   }
}


More information about the Digitalmars-d-learn mailing list