'Azure File Share : Find files with same first part of the filename C#

I have large number of files in a file share directory. Some of the files have their first part of the names are similar. I have to copy these files to another fileshare in Azure and incremently add a number to these files to differentiate the filenames.

sales_quarter_ABC_898_20220329_132948.csv named to sales_quarter_ABC_1.csv
sales_quarter_ABC_898_20220329_133000.csv named to sales_quarter_ABC_2.csv
sales_quarter_ABC_898_20220329_132922.csv named to sales_quarter_ABC_3.csv
sales_quarter_ABC_898_20220329_132951.csv named to sales_quarter_ABC_4.csv

The number of files can vary from 1 to couple of them .. it isn't definite. Using Regex to remove the number part from the destination filename and create a handle to the destination file

var name = Regex.Replace(file.Name, @"_(?=\d)[_\d]*", string.Empty);
 var destination = destFolder.GetFileClient(name);
  if (destination.Exists())
{
  Here I want to check if sales_quarter_ABC_1.csv exists then destination file name should be incremented to sales_quarter_ABC_2.csv
Now if 4 files already exist, the new file shuould be named sales_quarter_ABC_5.csv
}

Above I can search for a file with exact name and not using wildcard. I use Async API to copy files to the destination folder

await destination.StartCopyAsync(sourceFile.Uri)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source