Beispiel #1
0
        public static string CreatePath(DateTime dateStamp, string topNode, OrganizationFormat orgFormat)
        {
            switch (orgFormat)
            {
            case OrganizationFormat.ByYear:
            {
                var year = dateStamp.Year.ToString();
                return(topNode + "\\" + year + "\\");
            }

            case OrganizationFormat.ByMonth:
            {
                var month       = dateStamp.ToString("MMM");
                var monthNumber = dateStamp.ToString("MM");
                var year        = dateStamp.Year.ToString();
                return(topNode + "\\" + year + "\\" + monthNumber + "_" + month + "\\");
            }

            case OrganizationFormat.ByDay:
            {
                var month       = dateStamp.ToString("MMM");
                var monthNumber = dateStamp.ToString("MM");
                var year        = dateStamp.Year.ToString();
                var dayOfMonth  = dateStamp.Day.ToString().PadLeft(2, '0');
                return(topNode + "\\" + year + "\\" + monthNumber + "_" + month + "\\" + dayOfMonth + "\\");
            }

            case OrganizationFormat.Flat:
            default:
            {
                return(topNode + "\\");
            }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("*** START FILE BACKUP ***");

            Assembly ass  = Assembly.GetExecutingAssembly();
            string   path = System.IO.Path.GetDirectoryName(ass.Location);

            string excludeFile     = path + @"\exclude.txt";
            string extensionsFile  = path + @"\extensions.txt";
            var    sourceNode      = ConfigurationManager.AppSettings["SourceNode"];
            var    destinationNode = ConfigurationManager.AppSettings["DestinationNode"];
            var    useMetadata     = ConfigurationManager.AppSettings["UseMetadata"] == "true" ? true : false;
            var    logFile         = destinationNode + @"\log.txt";
            var    errorLogFile    = destinationNode + @"\error.txt";

            var excludeList = new List <string>();

            if (File.Exists(excludeFile))
            {
                var fileLines = File.ReadAllLines(excludeFile);
                excludeList = new List <string>(fileLines);
            }

            var extensionsList = new List <string>();

            if (File.Exists(extensionsFile))
            {
                var fileLines = File.ReadAllLines(extensionsFile);
                extensionsList = new List <string>(fileLines);
            }

            OrganizationFormat organizationFormat = (OrganizationFormat)Enum.Parse(typeof(OrganizationFormat), ConfigurationManager.AppSettings["OrganizationFormat"]);

            DirectoryInfo di = new DirectoryInfo(sourceNode);

            FileSystemScan.FullDirList(di, "*", excludeList);

            System.Console.WriteLine("Total File Count: {0}", FileSystemScan.files.Count);
            System.Console.WriteLine("Directory Count: {0}", FileSystemScan.DirectoryCount);
            System.Console.WriteLine("Folder Count: {0}", FileSystemScan.folders.Count);
            System.Console.WriteLine("Inaccessible Folder Count: {0}", FileSystemScan.inaccessfolders.Count);

            var fileList = FileSystemScan.files.Where(s => extensionsList.Contains(s.Extension.ToLower())).ToList();

            System.Console.WriteLine("Copy File Count: {0}", fileList.Count);

            foreach (var file in fileList)
            {
                var destinationPath = CreateDestinationPath(file, destinationNode, useMetadata, organizationFormat);

                CopyFileExactly(file.FullName, destinationPath, errorLogFile);
                //System.Console.WriteLine("File {0} {1} {2}", info.Name, info.CreationTime, fullDestPath);
            }
            System.Console.WriteLine("*** END FILE BACKUP ***");
            System.Console.Read();
        }
Beispiel #3
0
        public static string CreateDestinationPath(FileInfo sourcfileInfo, string destinationNode, bool useMetatData, OrganizationFormat orgFormat)
        {
            DateTime fileDate;

            if (useMetatData)
            {
                var takenDate = GetDateTakenFromImage(sourcfileInfo.FullName);

                if (takenDate != DateTime.MinValue)
                {
                    fileDate = takenDate;
                }
                else
                {
                    fileDate = sourcfileInfo.CreationTime > sourcfileInfo.LastWriteTime
                        ? sourcfileInfo.LastWriteTime
                        : sourcfileInfo.CreationTime;
                }
            }
            else
            {
                fileDate = sourcfileInfo.CreationTime > sourcfileInfo.LastWriteTime
                    ? sourcfileInfo.LastWriteTime
                    : sourcfileInfo.CreationTime;
            }

            switch (orgFormat)
            {
            case OrganizationFormat.ByYear:
            {
                var year = fileDate.Year.ToString();
                return(destinationNode + "\\" + year + "\\" + sourcfileInfo.Name);
            }

            case OrganizationFormat.ByMonth:
            {
                var month       = fileDate.ToString("MMM");
                var monthNumber = fileDate.ToString("MM");
                var year        = fileDate.Year.ToString();
                return(destinationNode + "\\" + year + "\\" + monthNumber + "_" + month + "\\" + sourcfileInfo.Name);
            }

            case OrganizationFormat.ByDay:
            {
                var month       = fileDate.ToString("MMM");
                var monthNumber = fileDate.ToString("MM");
                var year        = fileDate.Year.ToString();
                var dayOfMonth  = fileDate.Day.ToString().PadLeft(2, '0');
                return(destinationNode + "\\" + year + "\\" + monthNumber + "_" + month + "\\" + dayOfMonth + "\\" + sourcfileInfo.Name);
            }

            case OrganizationFormat.Flat:
            default:
            {
                return(destinationNode + "\\" + sourcfileInfo.Name);
            }
            }
        }