How to get file path with limited information from a datatable?

Hi all,

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?

Thanks in advance.

Hi @ATO ,

You can put it like this:

Syntax: “path”.Contains(“ref_code”)
Example: “RV-[46573823]-[Date]-[Time].xls”.Contains(“46573823”)
Output: True

Thanks,
Vinit Mhatre

Hi @Vinit_Mhatre ,

Thanks for your prompt reply.

Syntax: “path”.Contains(“ref_code”)

‘Ref code’ is a datatable variable, any idea how do I work with string array variable that stores Directory.GetFiles?

@ATO

Directory.GetFiles("FolderPath","*Refcodevalue*")(0) will give you the matching file inside the given FolderPath

Hope this helps

cheers

Hi @ATO,

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.

Folder 1

Folder 2

Output

Workflow

Xaml
Sequence5.xaml (15.1 KB)

Hi @RajKumar_DC

I can’t open the xaml file, error message as below

The item ‘C:\Users.…\Sequence5.xaml’ could not be opened: Document is invalid.

The screenshot resolution is abit small, some parts are blurry.

Would you mind to share again the xaml file?
Thanks in advance.

Hi @ATO,

Kindly find the image




Xaml
Sequence5.zip (2.4 KB)

To find the matching file paths in the second folder using the Ref code values from the first folder, you can follow these steps:

  1. Iterate through each Ref code value in the first folder’s file names.
  2. For each Ref code, search the second folder for a file with a matching Ref code in its name.
  3. If a matching file is found, add its file path to a list or collection.
  4. 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.

Hi @RajKumar_DC ,

Thanks for the photos and xaml file.
Managed to solve my problem using your steps.

Hi @sudheern ,

Thanks for your effort in giving the solution.
I haven’t try it but it should be working fine as I read through the steps.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.