Get a file name (after _ symbol) from a folder

I want to get a file names after _ symbol .
EX: File name = ABC45689_123456
Output should be = 123456

I am using below expression to get all the file names without extension .
Var1 =new System.IO.DirectoryInfo("FolderPath").GetFiles().Select(Function(file) Path.GetFileNameWithoutExtension(File.Name)).ToArray()

I want the expected output without using For Each activity. I am able to get it with For each
activity. (System.Text.RegularExpressions.Regex.Match(file.ToString,”(?<=_).*(\d)”).ToString.Trim)

Thank You

give a try at:

arrFileParts =

(From x in new System.IO.DirectoryInfo("FolderPath").GetFiles()
Let fn = Path.GetFileNameWithoutExtension(x.Name)
Let m = System.Text.RegularExpressions.Regex.Match(fn,strPatternVar)
Select v = m.Value.Trim).ToArray()

we would recommend additional to enhance:

  • handling case not matching the pattern
  • recheck and adapt the regex pattern
    strPatternVar = “YourRegexPatttern”
1 Like

If there is only ever one _ in the filename, just Split on the _ and take the second element (index 1)

Yes, that’s what I want to do but I am confused on variable types. File list I am getting in Array of String variable. Now How should I get those and split ?

I don’t understand this ? How should I achieve ?

Assign Activity
arrFileParts | String() a String Array =

(From x in new System.IO.DirectoryInfo("FolderPath").GetFiles()
Let fn = Path.GetFileNameWithoutExtension(x.Name)
Let m = System.Text.RegularExpressions.Regex.Match(fn,strPatternVar)
Select v = m.Value.Trim).ToArray()

you can start with your regex pattern

Still confused. Can you please send me an sample file ?

Find some training help for LINQ:

Thank you for sharing. IF you get a chance please send a sample it would be great help. Just want a code to get the file name after “_” symbol.

Please find the updated statement,

Var1 = new System.IO.DirectoryInfo("FolderPath").GetFiles().Select(Function(file) Path.GetFileNameWithoutExtension(File.Name).Split("_"c).Last).ToArray()
1 Like

Hey @Nithinkrishna Thank you so much. How should I know that the names are correct ? I mean I want to get those names and will compare those to my database data.

I just want that values in my log that the file name is 123.
I am using if condition to compare with my database data.

Hey @PALKUMARI_PATEL

So the above statement will return you the collection of file names.

You want to check whether each file is present in DB ?

Thanks
#nK

1 Like

Above statement is giving me files in Array list. I am checking particular file name from DB to this list if available. and that code I am using in IF , which is working fine.

I just want to get single file name from this array list , file A, file B, file C… without using for each activity.

For Each through the Array and split each item.

Okay @PALKUMARI_PATEL

Assuming strFilename is the file from DB.

You do the array.contains method.

Thanks
#nK

Why? This is what For Each is made for.

Otherwise you can just reference the array indexes ie myArray(0), myArray(1), etc but then you have to know how many items there are in the array and manage a counter etc. For Each does this all for you. It’s why it exists.

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