RePost: Help to convert a code to D
Andre Artus
andre.artus at gmail.com
Mon Aug 26 16:42:56 PDT 2013
On Monday, 26 August 2013 at 23:32:26 UTC, Andre Artus wrote:
> 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;
When testing I used timeLastModified as that created the greatest
variability with the files I had at my disposal. If you need to
use the file creation time then replace with timeCreated, as
follows:
auto sortedFiles = de.array.sort!((x, y) => x.timeCreated <
y.timeCreated);
foreach(DirEntry e; sortedFiles) {
auto tlm = e.timeCreated;
More information about the Digitalmars-d-learn
mailing list