I have found and remastered the following code to my needs. The code copies all spreadsheets from a source folder to a new folder based on an enumeration of spreadsheet file extensions.
However, it copies files twice (bug!), if they are in the root source folder or second level subfolder. Any other subfolders at third or fourth levels are only copied once, as intended.
What is the bug?
A second question, you see in the enumeration it has code to skip directory. I have tried deleting this code, because it does not seem necessary, and deleting the code does not affect the outcome. Is it necessary?
string argument1 = @"C:\Test";
string convert_directory = @"C:\Test";
// The input and output directory must be identical for the error to happen
if (argument3 == "Recursive=Yes")
{
var extensions = new List<string> { ".fods", ".ods", ".ots", ".xls", ".xlt", ".xlam", ".xlsb", ".xlsm", ".xlsx", ".xltm", ".xltx" };
// Create enumeration that only includes spreadsheet file extensions
var enumeration = new FileSystemEnumerable<FileSystemInfo>(argument1,(ref FileSystemEntry entry) => entry.ToFileSystemInfo(),new EnumerationOptions() { RecurseSubdirectories = true })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) =>
{
// Skip directories (is this necessary?)
if (entry.IsDirectory)
{
return false;
}
// End of skip directories
foreach (string extension in extensions)
{
var fileExtension = Path.GetExtension(entry.FileName);
if (fileExtension.EndsWith(extension, StringComparison.OrdinalIgnoreCase))
{
// Include the file if it matches extensions
return true;
}
}
// Doesn't match, exclude it
return false;
}
};
// Copy spreadsheets based on enumeration
foreach (var file in enumeration)
{
// Rename new copy
int copy_file_number = 1;
string new_filepath = convert_directory "\\" copy_file_number file.Extension;
while (File.Exists(new_filepath))
{
copy_file_number ;
new_filepath = convert_directory copy_file_number file.Extension;
}
// Copy
File.Copy(file.FullName, new_filepath);
}
CodePudding user response:
Thanks @marsze for rechecking my code and confirming it works. It made me think more about my debugging.
I used the same input directory as output directory. Then this error happens. If directories are different, it doesn't. Ideally this shouldn't be an issue, because files should only be copied once irregardless of what folder level, theey are in.
But my solution will be to make sure the input and output parameters cannot be identical.
CodePudding user response:
Your input and output folder are the same.
So you need to cache the list of files to copy before beginning the actual copying. Simply change the foreach
line to do this.
It also seems sensible to keep the copy_file_number
between loops so you don't have to keep checking the same files.
int copy_file_number = 1;
foreach (var file in enumeration.ToList())
{
// Rename new copy
string new_filepath = Path.Combine(convert_directory, copy_file_number file.Extension;
while (File.Exists(new_filepath))
{
copy_file_number ;
new_filepath = Path.Combine(convert_directory, copy_file_number file.Extension);
}
// Copy
File.Copy(file.FullName, new_filepath);
}