Newer
Older
wbackup / DuplicateCheck / Program.cs
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace DuplicateCheck
{
    class DuplicateCheckInfo
    {
        public string fileName;
        public byte[] hashValue;
        public long fileSize;

        public ulong HashKey
        {
            get
            {
                ulong hashKey = 0;
                for (int i = 0; i < sizeof(ulong); i++)
                {
                    hashKey = (hashKey << 8) ^ hashValue[i];
                }
                return hashKey;
            }
        }

        public DuplicateCheckInfo(string fileName)
        {
            this.fileName = fileName;
            var hash = SHA256.Create();
            using (FileStream fs = File.OpenRead(fileName))
            {
                fileSize = fs.Length;
                if (fileSize > 4096)
                {
                    hash.ComputeHash(fs);
                    hashValue = hash.Hash;
                }
            }
        }

        public bool IsMatch(DuplicateCheckInfo srce)
        {
            if (this.fileSize != srce.fileSize)
                return false;

            for (int i = 0;i < hashValue.Length;i ++)
            {
                if (hashValue[i] != srce.hashValue[i])
                    return false;
            }

            return true;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Duplicate Check Test Logic.");
            Dictionary<ulong, DuplicateCheckInfo> hashData = new Dictionary<ulong, DuplicateCheckInfo>();
            foreach (var item in Directory.GetFiles(@"D:\Projects\", "*", SearchOption.AllDirectories))
            {
                DuplicateCheckInfo info = new DuplicateCheckInfo(item);
                if (info.fileSize > 4096)
                {
                    ulong hashValue = info.HashKey;
                    if (hashData.TryGetValue(hashValue, out DuplicateCheckInfo srceInfo))
                    {
                        if (srceInfo.IsMatch(info))
                        {
                            Console.WriteLine("Duplicate {0}, {1}", item, srceInfo.fileName);
                            Console.WriteLine("Copy-Item -Path \"{0}\" -Destination \"{1}\"", srceInfo.fileName, item);
                            Console.WriteLine("Set-ItemProperty -Path \"{0}\" -Name LastWriteTime -Value \"{1}\"", item, File.GetLastWriteTime(item).ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                    else
                    {
                        hashData.Add(hashValue, info);
                    }
                }
            }
        }
    }
}