www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Help to convert a code to D

reply "Alexandre" <alebencz gmail.com> writes:
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...

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace Organizador
{
     class Organiza
     {
         private string[] MesesDoAno =
         {
           "Janeiro", "Fevereiro",
           "Marco", "Abril",
           "Maio", "Junho",
           "Julho", "Agosto",
           "Setembro", "Outubro",
           "Novembro", "Dezembro"
         };

         private string DirReceb = string.Empty;

         public Organiza()
         {
             CriaDiretorios();
             MoveArquivos();
         }

         private void CriaDiretorios()
         {
             DirReceb = "RECEBIDOS\\" + 
DateTime.Now.Year.ToString() + "\\";
             // Cria o diretorio "root"
             if (!Directory.Exists(DirReceb))
                 Directory.CreateDirectory(DirReceb);

             // cria os diretorios dos meses do ano
             for (int i = 0; i < 12; i++)
                 if (!Directory.Exists(DirReceb + MesesDoAno))
                     Directory.CreateDirectory(DirReceb + 
MesesDoAno[i]);

             // cria os diretorios com os dias dos meses e ano
             for (int i = 1; i <= 12; i++)
             {
                 string dia;
                 string mes;
                 var ano = DateTime.Now.Year;
                 var DiasDoMes = 
DateTime.DaysInMonth(DateTime.Now.Year, i);

                 if (i < 10)
                     mes = "0" + i.ToString();
                 else
                     mes = i.ToString();

                 for (int j = 1; j <= DiasDoMes; j++)
                 {
                     if (j < 10)
                         dia = "0" + j.ToString();
                     else
                         dia = j.ToString();

                     string StrDia = string.Format("{0}-{1}-{2}", 
dia, mes, ano);
                     string StrData = DirReceb + MesesDoAno[i-1] + 
"\\" + StrDia;

                     if (!Directory.Exists(StrData))
                         Directory.CreateDirectory(StrData);
                 }
             }
         }

         private void MoveArquivos()
         {
             string[] filters = new[] { "*.REM", "*.RET" };

             for (int i = 0; i < 2; i++)
             {
                 DirectoryInfo di = new 
DirectoryInfo(Directory.GetCurrentDirectory());
                 var files = di.GetFiles(filters[i]);

                 foreach (var fi in files)
                 {
                     var mes = fi.CreationTime.Month;
                     var strdt = 
fi.CreationTime.ToString("dd-MM-yyyy");

                     string DestDir = DirReceb + MesesDoAno[mes - 
1] + "\\" + strdt + "\\" + fi.Name;

                     File.Move(fi.Name, DestDir);
                 }
             }
         }
     }

     class Program
     {
         static void Main(string[] args)
         {
             try
             {
                 new Organiza();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
         }
     }
}


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 :)
Aug 26 2013
next sibling parent "Olivier Pisano" <olivier.pisano laposte.net> writes:
Hi Alexandre, welcome aboard !

This kind of question should ideally be asked in the d.learn 
forum, not here. Anyway, to create directories, you can use 
std.file mkdir and mkdirRecurse function. To get file 
information, there is the getAttributes function in the same 
module.

Note that for builing path names, you can use std.path buildPath 
function, which is portable between Windows and UNIX :

string DestDir = buildPath(DirReceb ~ MesesDoAno[mes - 1], strdt, 
fi.Name);

Cheers,

Olivier
Aug 26 2013
prev sibling next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 26/08/13 15:07, Alexandre wrote:
 I appreciate the help :)

There will be lots of people willing to offer it, but you'll probably get more (and better) feedback if you take these kinds of questions to the D.learn forum/mailing list. :-) http://forum.dlang.org/group/digitalmars.D.learn http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn
Aug 26 2013
prev sibling parent "Alexandre" <alebencz gmail.com> writes:
On Monday, 26 August 2013 at 13:19:59 UTC, Olivier Pisano wrote:
 Hi Alexandre, welcome aboard !

 This kind of question should ideally be asked in the d.learn 
 forum, not here. Anyway, to create directories, you can use 
 std.file mkdir and mkdirRecurse function. To get file 
 information, there is the getAttributes function in the same 
 module.

 Note that for builing path names, you can use std.path 
 buildPath function, which is portable between Windows and UNIX :

 string DestDir = buildPath(DirReceb ~ MesesDoAno[mes - 1], 
 strdt, fi.Name);

 Cheers,

 Olivier
On Monday, 26 August 2013 at 13:21:30 UTC, Joseph Rushton 
Wakeling wrote:
 On 26/08/13 15:07, Alexandre wrote:
 I appreciate the help :)

There will be lots of people willing to offer it, but you'll probably get more (and better) feedback if you take these kinds of questions to the D.learn forum/mailing list. :-) http://forum.dlang.org/group/digitalmars.D.learn http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn


Sorry for my mistake :( So, I will make that post on the correct forum :)
Aug 26 2013