Пример #1
0
        static void Main(string[] args)
        {
            //new Testes();
            //return;

            if (args.Count() == 0)
            {
                var CONFS = ConfiguracoesGlobais.GetConfs().DIRS;
                foreach (string str in CONFS)
                {
                    var obj = new WeekSorter(str);
                    obj.OrganizeFiles();
                }
            }
            else
            {
                Console.WriteLine("Parâmetros ainda não suportados . . . ");
            }

            Console.WriteLine("Press any key to continue . . . ");
            Console.ReadKey();
        }
Пример #2
0
        public void OrganizeFiles()
        {
            //Console.Out.WriteLine("ok");
            List <FileSystemInfo> ficheiros = GetFileList();


            DirectoryInfo di2 = new DirectoryInfo(alvo);

            foreach (FileSystemInfo fi in ficheiros)
            {
                bool aIgnorar = false;

                var CONFS1 = ConfiguracoesGlobais.GetConfs().IGNORAR1FILES;
                foreach (string str in CONFS1)
                {
                    if (String.Compare(fi.Name, str, true) == 0)
                    {
                        aIgnorar = true;
                        goto end;
                    }
                }


                var CONFS2 = ConfiguracoesGlobais.GetConfs().IGNORAR2REGEX;
                foreach (string str in CONFS2)
                {
                    if (Regex.Match(fi.Name, str).Success == true)
                    {
                        aIgnorar = true;
                        goto end;
                    }
                }

                if (String.Compare(fi.Name, "desktop.ini", true) == 0)
                {
                    aIgnorar = true;
                    goto end;
                }

                if (fi.Name.StartsWith(".") == true)
                {
                    aIgnorar = true;
                    goto end;
                }
end:

                if (aIgnorar)
                {
                    Console.WriteLine("IGNORADO (rule matched): " + fi.Name);
                }
                else
                {
                    DateTime lastTouchDate;
                    ObterUltimoAcessoOuMod(fi, out lastTouchDate);
                    var           weeksInYear = WeeksInYear(lastTouchDate);
                    DirectoryInfo destino2    = di2.CreateSubdirectory(lastTouchDate.Year + "-week" + weeksInYear);
                    //Console.Out.WriteLine(fi.Name + "->" + destino2.Name);


                    if (fi is FileInfo)
                    {
                        var destinoEmTralha = new FileInfo(destino2.ToString() + Path.DirectorySeparatorChar + fi.Name);
                        if (destinoEmTralha.Exists)
                        {
                            FileInfo Original  = (FileInfo)fi;
                            var      existente = GetChecksumBuffered(new FileStream(destinoEmTralha.ToString(), FileMode.Open));
                            var      candidato = GetChecksumBuffered(new FileStream(destino2.ToString() + Path.DirectorySeparatorChar + Original.ToString(), FileMode.Open));

                            Console.WriteLine("IGNORADO DUP: " + fi.Name);
                        }
                        else
                        {
                            try
                            {
                                ((FileInfo)fi).MoveTo(destinoEmTralha.ToString());
                                Console.Write("MOVIDO: ");
                                Console.WriteLine(fi.Name);
                            }
                            catch (Exception ex)
                            {
                                Console.Write("FALHOU A MOVER (excepção): ");
                                Console.WriteLine(fi.Name);
                            }
                        }
                    }
                    else if (fi is DirectoryInfo)
                    {
                        try
                        {
                            Console.Write("MOVIDO: ");
                            Console.WriteLine(fi.Name);

                            ((DirectoryInfo)fi).MoveTo(destino2.ToString() + Path.DirectorySeparatorChar + fi.Name);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("IGNORADO EXCEPCAO: " + fi.Name);
                        }
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
        }