Last active
August 29, 2015 14:24
-
-
Save sidewinder94/d15c9aab985e12d747fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Using :
https://gist.githubusercontent.com/DanielSWolf/0ab6a96899cc5377bf54/raw/c8a9bc27d7fc4375ea4eba645ad42735fbc299b9/ProgressBar.cs