using System;
using System.IO;
using Spectre.Console;
using System.Threading;
namespace SPT_AKI_Installer.Aki.Helper
{
public static class FileHelper
{
public static int totalFiles;
///
/// CopyDirectory will use old path and copy to new path and
/// asks if inner files/folders should be included
///
///
public static void CopyDirectory(string oldDir, string newDir, bool recursive)
{
AnsiConsole.Progress().Columns(
new TaskDescriptionColumn(),
new SpinnerColumn(),
new ElapsedTimeColumn()
).Start((ProgressContext context) =>
{
var dir = new DirectoryInfo(oldDir);
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (FileInfo f in dir.GetFiles())
{
totalFiles++;
}
foreach (DirectoryInfo subD in dirs)
{
foreach (FileInfo f in subD.GetFiles())
{
totalFiles++;
}
}
var task = context.AddTask("Copying files: ", true, totalFiles);
Directory.CreateDirectory(newDir);
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(newDir, file.Name);
file.CopyTo(targetFilePath, true);
}
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(newDir, subDir.Name);
AltCopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
});
//var dir = new DirectoryInfo(oldDir);
//if (!dir.Exists)
// throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
//DirectoryInfo[] dirs = dir.GetDirectories();
//Directory.CreateDirectory(newDir);
//foreach (FileInfo file in dir.GetFiles())
//{
// string targetFilePath = Path.Combine(newDir, file.Name);
// file.CopyTo(targetFilePath, true);
//}
//if (recursive)
//{
// foreach (DirectoryInfo subDir in dirs)
// {
// string newDestinationDir = Path.Combine(newDir, subDir.Name);
// CopyDirectory(subDir.FullName, newDestinationDir, true);
// }
//}
}
public static void AltCopyDirectory(string oldDir, string newDir, bool recursive)
{
var dir = new DirectoryInfo(oldDir);
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
DirectoryInfo[] dirs = dir.GetDirectories();
Directory.CreateDirectory(newDir);
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(newDir, file.Name);
file.CopyTo(targetFilePath, true);
}
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(newDir, subDir.Name);
AltCopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
///
/// DeleteFiles will use a type to look for, the path
/// and if all inner files/folders should be included
///
///
/// Types are "file" or "folder" as a string
///
public static void DeleteFile(string type, string filePath, bool allFolders = false)
{
// type = "file" or "folder"
if (string.Equals(type, "file", StringComparison.OrdinalIgnoreCase))
{
File.Delete(filePath);
}
if (string.Equals(type, "folder", StringComparison.OrdinalIgnoreCase))
{
Directory.Delete(filePath, allFolders);
}
}
///
/// finds file based on Path and File name
///
///
///
/// String or null
public static string FindFile(string path, string name)
{
string[] filePaths = Directory.GetFiles(path);
foreach (string file in filePaths)
{
if (file.Contains(name))
{
return file;
}
}
return null;
}
///
/// Finds folder with name supplied, out = directory for extracted patch folder
///
///
///
/// bool
public static bool FindFolder(string patchRef, string targetPath, out DirectoryInfo dir)
{
var patchInfo = new FileInfo(patchRef);
var patchName = patchInfo.Name.Replace(patchInfo.Extension, "");
var path = new DirectoryInfo(Path.Join(targetPath, patchName));
if (path.Exists)
{
dir = path;
return true;
}
dir = null;
return false;
}
}
}