コード例 #1
0
ファイル: Program.cs プロジェクト: philprett/pcopy
 static int Main(string[] args)
 {
     try
     {
         Parameters parameters = new Parameters(args);
         if (!parameters.Valid)
         {
             Console.Out.WriteLine(Parameters.Syntax);
             return(1);
         }
         else
         {
             PFolder folder = new PFolder(parameters.SourceDirectory, parameters.DestinationDirectory);
             folder.Copy(parameters);
         }
     }
     catch (InvalidParameterException ex)
     {
         Console.Out.WriteLine(ex.Message);
         Console.Out.WriteLine(Parameters.Syntax);
         return(1);
     }
     return(0);
 }
コード例 #2
0
        public void Copy(Parameters parameters)
        {
            if (!LongDirectory.Exists(DestPath))
            {
                LongDirectory.CreateDirectory(DestPath);
            }

            if (parameters.Verbose)
            {
                Console.Out.WriteLine("{0}", SourcePath);
            }
            string[] sourceFilePaths = LongDirectory.GetFiles(SourcePath);
            string[] destFilePaths   = LongDirectory.GetFiles(DestPath);

            foreach (string sourceFilePath in sourceFilePaths)
            {
                string copyFileComment = string.Empty;
                bool   copyFile        = false;
                string sourceFilename  = LongFile.GetName(sourceFilePath);
                string destFilePath    = LongFile.Combine(DestPath, sourceFilename);
                if (destFilePaths.FirstOrDefault(f => f.Equals(destFilePath, StringComparison.CurrentCultureIgnoreCase)) == null)
                {
                    copyFile        = true;
                    copyFileComment = "new   ";
                }
                else
                {
                    DateTime sourceModified = LongFile.GetLastWriteTime(sourceFilePath);
                    DateTime destModified   = LongFile.GetLastWriteTime(destFilePath);
                    int      modComp        = sourceModified.CompareTo(destModified);
                    if (modComp > 0)
                    {
                        copyFile        = true;
                        copyFileComment = "newer ";
                    }
                    else if (modComp < 0)
                    {
                        copyFile        = true;
                        copyFileComment = "older ";
                    }
                    else
                    {
                        copyFileComment = "same  ";
                    }
                }

                if (copyFile)
                {
                    if (parameters.Verbose)
                    {
                        Console.Out.WriteLine("     {0} : {1} ", copyFileComment, sourceFilename);
                    }
                    try
                    {
                        LongFile.Copy(sourceFilePath, destFilePath, true);
                    }
                    catch (Exception ex)
                    {
                        Console.Out.WriteLine("{0}", ex.Message);
                    }
                }
            }

            if (parameters.Recursive)
            {
                string[] sourceDirs = LongDirectory.GetDirectories(SourcePath);
                foreach (string sourceDir in sourceDirs)
                {
                    string sourceName = LongFile.GetName(sourceDir);
                    string destDir    = LongFile.Combine(DestPath, sourceName);
                    if (!LongDirectory.Exists(destDir))
                    {
                        LongDirectory.CreateDirectory(destDir);
                    }

                    PFolder pFolder = new PFolder(sourceDir, destDir);
                    pFolder.Copy(parameters);
                }
            }

            if (SourcePath == parameters.SourceDirectory)
            {
                if (parameters.RequireEnterToExit)
                {
                    Console.Out.WriteLine("Press enter to exit");
                    Console.In.ReadLine();
                }
            }
        }