I have two folders to work on. I am trying to get the matching file paths in 2nd folder using the ‘Ref code’ from 1st folder.
File name format in 1st folder = RV_ASNUp-[Ref code]-[Date]-[Time].xls
File name format in 2nd folder = RV-[Ref code]-[Date]-[Time].xls
In first folder, I managed to extract only the ‘Ref code’ and store to a datatable using Directory.Getfiles and Getfilename with substring and split method.
How can I get only the file path in 2nd folder that match with ‘Ref code’ extracted from 1st folder?
As per my understand collect the RefCode value from Folder1 files path then check with Folder2 files collected RefCode is Contains with path if yes add to data table.
To find the matching file paths in the second folder using the Ref code values from the first folder, you can follow these steps:
Iterate through each Ref code value in the first folder’s file names.
For each Ref code, search the second folder for a file with a matching Ref code in its name.
If a matching file is found, add its file path to a list or collection.
After all Ref code values have been processed, you should have a list of file paths for the matching files in the second folder.
Here is some sample C# code that demonstrates this approach:
using System.IO;
// Assume that firstFolderPath and secondFolderPath contain the paths to the first and second folders, respectively.
string firstFolderPath = “C:\FirstFolder”;
string secondFolderPath = “C:\SecondFolder”;
// Get all of the file paths in the first folder.
string firstFolderFilePaths = Directory.GetFiles(firstFolderPath);
// Create a list to store the matching file paths in the second folder.
List matchingFilePaths = new List();
// Iterate through each file path in the first folder.
foreach (string firstFolderFilePath in firstFolderFilePaths)
{
// Extract the Ref code from the file name.
string fileName = Path.GetFileName(firstFolderFilePath);
string refCode = fileName.Split(‘-’)[1];
// Search for a matching file in the second folder.
string[] secondFolderFilePaths = Directory.GetFiles(secondFolderPath, "RV-" + refCode + "*");
// If a matching file was found, add it to the list.
if (secondFolderFilePaths.Length > 0)
{
matchingFilePaths.Add(secondFolderFilePaths[0]);
}
}
// The matchingFilePaths list now contains the file paths for all of the matching files in the second folder.
This code first gets all of the file paths in the first folder using the Directory.GetFiles method. It then iterates through each file path, extracts the Ref code from the file name, and searches the second folder for a file with a matching Ref code using the Directory.GetFiles method. If a matching file is found, its file path is added to the matchingFilePaths list. After all file paths in the first folder have been processed, the matchingFilePaths list will contain the file paths for all of the matching files in the second folder.
I hope this helps! Let me know if you have any questions or if you would like further clarification on any of the steps.