Reputation:
I want to copy all files from the current folder to another folder inside that. but excluding few files which is a .exe file and a .dll file.
this is the code i have right now, this copies every file from current to the other folder. i have searched and got few answers to exclude based on the extensions but that isnt what i was looking for. i want to exclude based on their name so that i can exclude more files later on.
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
Upvotes: 0
Views: 1263
Reputation: 391
Try, that should work:
async Task<void> Copy(List<string> ExcludedNamesWithoutExtensions)
{
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
{
bool CanCopy = true;
string[] array = newPath.Split('\\')[newPath.Split('\\').Length - 1].Split('.');
string name = array[array.Length - 1];
foreach (string s in ExcludedNamesWithoutExtensions)
{
if (name != s)
{
CanCopy = false;
break;
}
}
if (CanCopy)
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
}
}
Upvotes: 0
Reputation: 4639
This should work:
HashSet<string> namesToExclude = new HashSet<string>()
{
"bla.dll",
"blubb.exe"
};
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
{
if (!namesToExclude.Contains(Path.GetFileName(newPath)))
{
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
}
}
Oh and as a sidenote, your code will not copy if there are only directories and no files inside the root directory, you might want to check for directories as well. And subdirectories will be a problem as well. You could use recursion to fix this.
Upvotes: 1