How to create a new List with each set of matching values

Hi,

I have a list of files that I want to upload to a system. I need some help to figure out how to create a new List with each set of matching file name so that I can do a mass upload, and then if one of the files fails during a batch upload then I want to add it to a separate list to perform individual upload later.

For example:

I have the following files in a local folder:

1.1.1-P01.txt
1.1.1-P02.docx
1.1.2-P01.txt
5.1.1-P01.txt
5.1.1-P02.txt
5.1.1-P03.pdf

Expected result:

New list:
1.1.1-P01.txt
1.1.1-P02.docx
New list:
1.1.2-P01.txt
New list:
5.1.1-P01.txt
5.1.1-P02.txt
5.1.1-P03.pdf

Now I have three batch to upload, but if let’s say 1.1.1-P01.txt and 5.1.1-P01.txt failed during mass upload then I want to create a separate list to store the files for individual upload (The reason for this is that if one of the files failed during a mass upload, the entire batch will stop).

My list for individual upload would look like this:

New list:
1.1.1-P01.txt
1.1.1-P02.docx
5.1.1-P01.txt
5.1.1-P02.txt
5.1.1-P03.pdf

Finally, I will perform individual upload one by one from that collection.

In this example I only have three batch. But I could have more too.

@Yoichi
@ppr

Can you please take a look at this?

Hi,

In this case, I recommend to use Dictionary as the following.

dict = arrFiles.GroupBy(Function(f) System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"^.*(?=-)").Value).ToDictionary(Function(g) g.Key,Function(g) g.ToArray)

Sample20221216-1.zip (3.9 KB)

Regards,

Thank you so much Yoichi. It works as expected.

I have one question though.

Inside the for Each item in Dict loop, the message box you setup is this:
“Submit file list”+vbcrlf+String.Join(vbcrlf,item.Value)

I want to change the message box to Log Message and display each item.Value from Dict.

String.Join(vbcrlf,item.Value) merges item (Array of String) into one line. I need to perform some actions with each file inside item (Array of String). So how do I get individual value from item (Array of String)?

Hi,

We can achieve it using ForEach (double loop) as the following.

Regards

Thanks again! I forgot to change TypeArgument to String in the second For Each loop…

1 Like

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