www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RePost: Help to convert a code to D

reply "Alexandre" <alebencz gmail.com> writes:
Hi :)


to D...

with all days of month of specific year...



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 "bearophile" <bearophileHUGS lycos.com> writes:
Alexandre:


If you don't receive answers, then I suggest you to cut your program in perpendicular slices, and try to translate them in D, one after the other, and to show them here, hoping for comments. In a technical forum people are usually happier when you do some work yourself. Bye, bearophile
Aug 26 2013
prev sibling next sibling parent reply "Andre Artus" <andre.artus gmail.com> writes:
On Monday, 26 August 2013 at 13:30:38 UTC, Alexandre wrote:
 Hi :)


 to D...

 with all days of month of specific year...



 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 :)
Hi Alexandre, Would you mind explaining in English what it is that you would like to achieve. I cannot read [what I assume is] Portuguese, but if you can explain what you wan't I will try my best to help.
Aug 26 2013
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
  I wonder if any of this would make any more sense if you threw 
it through Google translate.. (maybe the comments and months?)



On Monday, 26 August 2013 at 20:14:34 UTC, Andre Artus wrote:
 Hi Alexandre,

 Would you mind explaining in English what it is that you would 
 like to achieve. I cannot read [what I assume is] Portuguese, 
 but if you can explain what you want I will try my best to help.
From what I can tell he's making a directory either per day of the month, or per month, then moving all files last created/modified to those directories. On Monday, 26 August 2013 at 13:30:38 UTC, Alexandre wrote:
                  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);
                  }
              }
          }
      }
Aug 26 2013
parent "Andre Artus" <andre.artus gmail.com> writes:
On Monday, 26 August 2013 at 21:43:20 UTC, Era Scarecrow wrote:
  I wonder if any of this would make any more sense if you threw 
 it through Google translate.. (maybe the comments and months?)
I did just that :)
 On Monday, 26 August 2013 at 20:14:34 UTC, Andre Artus wrote:
 Hi Alexandre,

 Would you mind explaining in English what it is that you would 
 like to achieve. I cannot read [what I assume is] Portuguese, 
 but if you can explain what you want I will try my best to 
 help.
From what I can tell he's making a directory either per day of the month, or per month, then moving all files last created/modified to those directories.
I always try to understand the person's intention. The obvious answer isn't always the best solution. Someone may be asking "what route should I take to sail my lilo [inflatable raft] to Perth?"
Aug 26 2013
prev sibling parent reply "Andre Artus" <andre.artus gmail.com> writes:
On Monday, 26 August 2013 at 13:30:38 UTC, Alexandre wrote:
 Hi :)


 to D...

 with all days of month of specific year...


-- 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 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) { 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" ]; 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. } }
Aug 26 2013
parent "Andre Artus" <andre.artus gmail.com> writes:
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 :)


 to D...

 with all days of month of specific year...


-- 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 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) { 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" ]; 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;
Aug 26 2013