Scan for duplicate C# scripts
This version of the script prompts the user to enter a starting path to scan for duplicate C# script files with the same name. The script uses a dictionary to store a list of files with the same name and compares their contents using an MD5 hash to determine if they are identical. The script then prompts the user to confirm before deleting any duplicate files, excluding the most recent version of the file. If no path is entered, the script defaults to scanning the D:\myfiles\
directory. The script waits for user input before starting and pauses at the end.
-----------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RemoveDuplicateScripts
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Remove Duplicate Scripts tool.");
Console.WriteLine("This tool will scan your specified directory and remove duplicate C# scripts with the same name.");
Console.WriteLine("You will be prompted for confirmation before any file is deleted.");
Console.WriteLine();
// allow user to enter the starting path to scan
Console.Write("Enter the starting path to scan (e.g. D:\\myfiles): ");
string startingPath = Console.ReadLine();
if (string.IsNullOrWhiteSpace(startingPath))
{
startingPath = "D:\\myfiles\\";
Console.WriteLine($"No path entered, defaulting to {startingPath}");
}
else
{
startingPath = startingPath.TrimEnd('\\') + "\\";
}
List<string> extensions = new List<string>() { ".cs" }; // specify file extensions to scan
Dictionary<string, List<FileInfo>> files = new Dictionary<string, List<FileInfo>>();
// scan the subdirectories of the starting path for files with specified extensions
foreach (string extension in extensions)
{
foreach (string filePath in Directory.GetFiles(startingPath, "*" + extension, SearchOption.AllDirectories))
{
// skip files that you don't have permission to access
try
{
FileInfo fileInfo = new FileInfo(filePath);
string fileName = fileInfo.Name.ToLower();
if (files.ContainsKey(fileName))
{
files[fileName].Add(fileInfo);
}
else
{
files.Add(fileName, new List<FileInfo> { fileInfo });
}
}
catch (UnauthorizedAccessException)
{
Console.WriteLine($"Skipping file: {filePath} (access denied)");
}
}
}
// remove duplicate files
foreach (KeyValuePair<string, List<FileInfo>> pair in files)
{
if (pair.Value.Count > 1)
{
Console.WriteLine($"Duplicate file found: {pair.Key}");
foreach (FileInfo fileInfo in pair.Value.Skip(1))
{
// prompt for confirmation before deleting the file
Console.Write($"Do you want to delete {fileInfo.FullName}? (y/n): ");
string response = Console.ReadLine();
if (response.ToLower() == "y")
{
File.Delete(fileInfo.FullName);
Console.WriteLine("File deleted.");
}
}
}
}
Console.WriteLine("Done.");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment