Skip to content

Instantly share code, notes, and snippets.

@sidewinder94
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidewinder94/d15c9aab985e12d747fc to your computer and use it in GitHub Desktop.
Save sidewinder94/d15c9aab985e12d747fc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CopyCat
{
class Program
{
private static long _fileCount = 0;
private static long _byteCount = 0;
private static long _byteProgress = 0;
private static ProgressBar _progressCount = new ProgressBar();
static void Main(string[] args)
{
Directory.Exists(args[0]);
Directory.Exists(args[1]);
FileDiscovery(args[0]);
FileCopy(args[0], args[1]);
Console.ReadLine();
}
static void FileCopy(String source, String dest)
{
try
{
foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.TopDirectoryOnly))
{
try
{
if (file == null) continue;
var oFile = File.OpenRead(file);
var dFile = File.Open(Path.Combine(dest, Path.GetFileName(file)), FileMode.Create,
FileAccess.ReadWrite);
oFile.CopyTo(dFile, 104857600);
oFile.Close();
dFile.Flush();
dFile.Close();
_byteProgress += new FileInfo(file).Length;
_progressCount.Report((double)_byteProgress / (double)_byteCount);
}
catch (Exception e)
{
Console.WriteLine("[COPY][ERROR] : Couldn't copy file : {0} => {1}", file, e.Message);
}
}
foreach (var directory in Directory.EnumerateDirectories(source, "*", SearchOption.TopDirectoryOnly))
{
if (directory == @"G:\$RECYCLE.BIN") continue;
var dir = Path.GetFileName(directory);
if (!Directory.Exists(Path.Combine(dest, dir)))
{
Directory.CreateDirectory(Path.Combine(dest, dir));
}
FileCopy(directory, Path.Combine(dest, dir));
}
}
catch (Exception exception)
{
Console.WriteLine("[COPY][WARNING] : Couldn't open directory : {0}", source);
}
}
static void FileDiscovery(String dir)
{
try
{
foreach (var file in Directory.EnumerateFiles(dir, "*", SearchOption.TopDirectoryOnly))
{
_fileCount++;
_byteCount += new FileInfo(file).Length;
}
foreach (var directory in Directory.EnumerateDirectories(dir, "*", SearchOption.TopDirectoryOnly))
{
FileDiscovery(directory);
}
}
catch (Exception exception)
{
Console.WriteLine("[DISCOVERY][WARNING] : Couldn't open directory : {0}", dir);
}
}
static String HumanReadableByteCount(long bytes, Boolean si = false, int precision = 2)
{
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int)(Math.Log(bytes) / Math.Log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE")[(exp - 1)] + (si ? "" : "i");
return String.Format("{0} {1}{2}", Math.Round(bytes / Math.Pow(unit, exp), precision), pre, si ? "b" : "B");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment