I want to write a small piece of code that checks a certain column in excel for the file name, finds it and then renames it.
This is how I have done it:
int colNo = oWorksheet.UsedRange.Columns.Count;
int rowNo = oWorksheet.UsedRange.Rows.Count;
// read the value into an array.
object[,] array = oWorksheet.UsedRange.Value;
for (int j = 1; j <= colNo; j++)
{
for (int i = 1; i <= rowNo; i++)
{
if (array[i, j] != null)
{
if (array[i, j].ToString() == "vin")
{
for (int m = i + 1; m <= rowNo; m++)
{
try
{
string name = array[m, j].ToString(); //accessing the name to be renamed
string invoice_name = array[m, j + 7].ToString(); // invoice_name
invoice_name.Trim();
string directoryPath = @"C:\Users\User\Documents\Invoices\";
invoice_name = invoice_name.Replace(" ", "");
string[] files = new string[0];
files = System.IO.Directory.GetFiles(directoryPath, "*" + invoice_name + ".pdf");
// then do something with the found files
}
What my code is doing is its searching for the files located in directoryPath and then finding them with the same value that is stored in 'invoice_name'. Since I am using the * wildcard, it is searching for all files which have the same name as the value in 'invoice_name'.
However if there is a file found which has the value matching 80% of the value stored in 'invoice_name' then the Directory.GetFiles method returns a null. For instance if the value stored in 'invoice_name' is 'Michael' then the Directory.GetFiles returns all files which have the name 'Michael' or Michael+something.
However it does NOT return files which have names 'Michae', 'Micha', 'Mich' or anything less than 'Michael'.
Can someone please suggest something? I've been looking into existing solutions/questions and I found this: How to find folders and files by its partial name c# but it doesn't help me, I tried it out.